--[[*
 * Compile a regular expression from the `state` object returned by the
 * [parse()](#parse) method.
 *
 * @param {Object} `state`
 * @param {Object} `options`
 * @param {Boolean} `returnOutput` Intended for implementors, this argument allows you to return the raw output from the parser.
 * @param {Boolean} `returnState` Adds the state to a `state` property on the returned regex. Useful for implementors and debugging.
 * @return {RegExp}
 * @api public
 ]]


picomatch.compileRe = function(state: Object, options: Object?, returnOutput_: boolean?, returnState_: boolean?): RegExp
	local returnOutput = returnOutput_ or false
	local returnState = returnState_ or false

	if returnOutput == true then
		return state.output
	end

	local opts: Object = options or {}
	local prepend = if Boolean.toJSBoolean(opts.contains) then "" else "^"
	local append = if Boolean.toJSBoolean(opts.contains) then "" else "$"

	local source = ("%s(?:%s)%s"):format(prepend, tostring(state.output), append)
	if typeof(state) == "table" and state.negated == true then
		source = ("^(?!%s).*$"):format(source)
	end

	local regex = picomatch.toRegex(source, options)
	if returnState == true then
		regex.state = state
	end

	return regex
end
