const simpleTypes = ['number', 'boolean', 'function', 'symbol'];
function isRegexp(value) {
    return Object.prototype.toString.call(value) === '[object RegExp]';
}
function isObject(value) {
    const type = typeof value;
    return value !== null && (type === 'object' || type === 'function');
}
export function objectToPhpArray(input, options, pad = '') {
    const seen = [];
    return (function stringify(input, options = {}, pad = '') {
        options.indent = options.indent || '\t';
        const tokens = {
            newLine: '\n',
            newLineOrSpace: '\n',
            pad,
            indent: pad + options.indent,
        };
        const expandWhiteSpace = (string) => {
            if (options.inlineCharacterLimit === undefined) {
                return string;
            }
            const oneLined = string
                .replace(new RegExp(tokens.newLine, 'g'), '')
                .replace(new RegExp(tokens.newLineOrSpace, 'g'), ' ')
                .replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), '');
            if (oneLined.length <= options.inlineCharacterLimit) {
                return oneLined;
            }
            return string
                .replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n')
                .replace(new RegExp(tokens.pad, 'g'), pad)
                .replace(new RegExp(tokens.indent, 'g'), pad + options.indent);
        };
        if (seen.indexOf(input) !== -1) {
            return '"Circular"';
        }
        if (input === null)
            return 'NULL';
        if (input === undefined || simpleTypes.includes(typeof input) || isRegexp(input)) {
            return String(input);
        }
        if (input instanceof Date) {
            return `date('${input.toISOString()}')`;
        }
        if (Array.isArray(input)) {
            if (input.length === 0) {
                return 'array()';
            }
            seen.push(input);
            const ret = 'array(' +
                tokens.newLine +
                input
                    .map((el, i) => {
                    const eol = input.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
                    let value = stringify(el, options, pad + options.indent);
                    if (options.transform) {
                        value = options.transform(input, i, value);
                    }
                    return tokens.indent + value + eol;
                })
                    .join('') +
                tokens.pad +
                ')';
            seen.pop();
            return expandWhiteSpace(ret);
        }
        if (isObject(input)) {
            let objKeys = Object.keys(input);
            if (options.filter) {
                objKeys = objKeys.filter((el) => options.filter?.(input, el));
            }
            if (objKeys.length === 0) {
                return Array.isArray(input) ? `array()` : `(object)[]`;
            }
            seen.push(input);
            const ret = 'array(' +
                tokens.newLine +
                objKeys
                    .map((el, i) => {
                    const eol = objKeys.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
                    const isSymbol = typeof el === 'symbol';
                    const isClassic = !isSymbol && /^[a-z$_][a-z$_0-9]*$/i.test(el);
                    const key = isSymbol || isClassic ? el : stringify(el, options);
                    let value = stringify(input[el], options, pad + options.indent);
                    if (options.transform) {
                        value = options.transform(input, el, value);
                    }
                    return `${tokens.indent}"${String(key)}" => ${value}${eol}`;
                })
                    .join('') +
                tokens.pad +
                ')';
            seen.pop();
            return expandWhiteSpace(ret);
        }
        input = String(input).replace(/[\r\n]/g, (x) => (x === '\n' ? '\\n' : '\\r'));
        if (options.singleQuotes === false) {
            input = input.replace(/"/g, '\\"');
            return `"${input}"`;
        }
        input = input.replace(/\\?'/g, "\\'");
        return `"${input}"`;
    })(input, options, pad);
}
//# sourceMappingURL=utils.js.map