"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const plugin = () => {
    return {
        version: 2.2,
        compileSFCStyle(_lang, style) {
            return {
                imports: [...parseCssImports(style)],
                bindings: [...parseCssBindings(style)],
                classNames: [...parseCssClassNames(style)],
            };
        },
    };
};
exports.default = plugin;
const cssImportRE = /(?<=@import\s+url\()(["']?).*?\1(?=\))|(?<=@import\b\s*)(["']).*?\2/g;
const cssBindingRE = /\bv-bind\s*\(/g;
const cssClassNameRE = /\.[a-z_][-\w]*(?=[\s.,+~>:#)[{])/gi;
const commentRE = /(?<=\/\*)[\s\S]*?(?=\*\/)|(?<=\/\/)[\s\S]*?(?=\n)/g;
const fragmentRE = /(?<={)[^{]*(?=(?<!\\);)/g;
function* parseCssImports(css) {
    for (const match of css.matchAll(cssImportRE)) {
        yield trimQuotes(match[0], match.index);
    }
}
function* parseCssBindings(css) {
    css = fillBlank(css, commentRE);
    for (const match of css.matchAll(cssBindingRE)) {
        const start = match.index + match[0].length;
        const end = lexBinding(css, start);
        if (end !== null) {
            yield trimQuotes(css.slice(start, end), start, false);
        }
    }
}
var LexerState;
(function (LexerState) {
    LexerState[LexerState["inParens"] = 0] = "inParens";
    LexerState[LexerState["inSingleQuoteString"] = 1] = "inSingleQuoteString";
    LexerState[LexerState["inDoubleQuoteString"] = 2] = "inDoubleQuoteString";
})(LexerState || (LexerState = {}));
// https://github.com/vuejs/core/blob/c0606e91798c8dca4f33d101e1dd836d672592c1/packages/compiler-sfc/src/style/cssVars.ts#L93
function lexBinding(content, start) {
    let state = LexerState.inParens;
    let parenDepth = 0;
    for (let i = start; i < content.length; i++) {
        const char = content.charAt(i);
        switch (state) {
            case LexerState.inParens:
                if (char === `'`) {
                    state = LexerState.inSingleQuoteString;
                }
                else if (char === `"`) {
                    state = LexerState.inDoubleQuoteString;
                }
                else if (char === `(`) {
                    parenDepth++;
                }
                else if (char === `)`) {
                    if (parenDepth > 0) {
                        parenDepth--;
                    }
                    else {
                        return i;
                    }
                }
                break;
            case LexerState.inSingleQuoteString:
                if (char === `'`) {
                    state = LexerState.inParens;
                }
                break;
            case LexerState.inDoubleQuoteString:
                if (char === `"`) {
                    state = LexerState.inParens;
                }
                break;
        }
    }
    return null;
}
function* parseCssClassNames(css) {
    css = fillBlank(css, commentRE, fragmentRE);
    for (const match of css.matchAll(cssClassNameRE)) {
        yield { text: match[0], offset: match.index };
    }
}
function fillBlank(css, ...patterns) {
    for (const pattern of patterns) {
        css = css.replace(pattern, match => ' '.repeat(match.length));
    }
    return css;
}
function trimQuotes(text, offset, trim = true) {
    let start = 0;
    let end = text.length;
    if (trim || text.includes('"') || text.includes("'")) {
        while (start < text.length && !text[start]?.trim()) {
            start++;
        }
        while (end >= 0 && !text[end - 1]?.trim()) {
            end--;
        }
    }
    if (text[start] === '"' && text[end - 1] === '"'
        || text[start] === "'" && text[end - 1] === "'") {
        start++;
        end--;
    }
    return {
        text: text.slice(start, end),
        offset: offset + start,
    };
}
//# sourceMappingURL=vue-style-css.js.map