'use strict';
const TEMPLATE_REGEX = /(?:\\(u[a-f\d]{4}|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
const ESCAPE_REGEX = /\\(u[a-f\d]{4}|x[a-f\d]{2}|.)|([^\\])/gi;

const ESCAPES = new Map([
    ['n', '\n'],
    ['r', '\r'],
    ['t', '\t'],
    ['b', '\b'],
    ['f', '\f'],
    ['v', '\v'],
    ['0', '\0'],
    ['\\', '\\'],
    ['e', '\u001B'],
    ['a', '\u0007']
]);

function unescape(c) {
    if ((c[0] === 'u' && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
        return String.fromCharCode(parseInt(c.slice(1), 16));
    }

    return ESCAPES.get(c) || c;
}

function parseArguments(name, args) {
    const results = [];
    const chunks = args.trim().split(/\s*,\s*/g);
    let matches;

    for (const chunk of chunks) {
        if (!isNaN(chunk)) {
            results.push(Number(chunk));
        } else if ((matches = chunk.match(STRING_REGEX))) {
            results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, chr) => escape ? unescape(escape) : chr));
        } else {
            throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
        }
    }

    return results;
}

function parseStyle(style) {
    STYLE_REGEX.lastIndex = 0;

    const results = [];
    let matches;

    while ((matches = STYLE_REGEX.exec(style)) !== null) {
        const name = matches[1];

        if (matches[2]) {
            const args = parseArguments(name, matches[2]);
            results.push([name].concat(args));
        } else {
            results.push([name]);
        }
    }

    return results;
}

function buildStyle(chalk, styles) {
    const enabled = {};

    for (const layer of styles) {
        for (const style of layer.styles) {
            enabled[style[0]] = layer.inverse ? null : style.slice(1);
        }
    }

    let current = chalk;
    for (const styleName of Object.keys(enabled)) {
        if (Array.isArray(enabled[styleName])) {
            if (!(styleName in current)) {
                throw new Error(`Unknown Chalk style: ${styleName}`);
            }

            if (enabled[styleName].length > 0) {
                current = current[styleName].apply(current, enabled[styleName]);
            } else {
                current = current[styleName];
            }
        }
    }

    return current;
}

module.exports = (chalk, tmp) => {
    const styles = [];
    const chunks = [];
    let chunk = [];

    // eslint-disable-next-line max-params
    tmp.replace(TEMPLATE_REGEX, (m, escapeChar, inverse, style, close, chr) => {
        if (escapeChar) {
            chunk.push(unescape(escapeChar));
        } else if (style) {
            const str = chunk.join('');
            chunk = [];
            chunks.push(styles.length === 0 ? str : buildStyle(chalk, styles)(str));
            styles.push({inverse, styles: parseStyle(style)});
        } else if (close) {
            if (styles.length === 0) {
                throw new Error('Found extraneous } in Chalk template literal');
            }

            chunks.push(buildStyle(chalk, styles)(chunk.join('')));
            chunk = [];
            styles.pop();
        } else {
            chunk.push(chr);
        }
    });

    chunks.push(chunk.join(''));

    if (styles.length > 0) {
        const errMsg = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
        throw new Error(errMsg);
    }

    return chunks.join('');
};