"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isStScopeStatement = exports.StylablePublicApi = exports.hooks = exports.diagnostics = void 0;
const feature_1 = require("./feature");
const selector_1 = require("../helpers/selector");
const postcss = __importStar(require("postcss"));
exports.diagnostics = {
// INVALID_SCOPING: createDiagnosticReporter(
//     '11009',
//     'error',
//     () => '"@st-scope" requires a valid selector or empty value'
// ),
};
// HOOKS
exports.hooks = (0, feature_1.createFeature)({
    analyzeAtRule({ context, atRule, analyzeRule }) {
        if (!isStScopeStatement(atRule)) {
            return;
        }
        // notice: any value from params would be taken as a scoping
        // selector to be prepended to nested selectors
        analyzeRule(postcss.rule({
            selector: atRule.params,
            source: atRule.source,
        }), {
            isScoped: true,
            originalNode: atRule,
        });
        context.meta.scopes.push(atRule);
    },
    prepareAST({ node, toRemove }) {
        // called with experimentalSelectorInference=false
        // flatten @st-scope before transformation
        if (isStScopeStatement(node)) {
            flattenScope(node);
            toRemove.push(() => node.replaceWith(node.nodes || []));
        }
    },
    transformAtRuleNode({ context: { meta, inferredSelectorMixin }, atRule, transformer }) {
        if (isStScopeStatement(atRule)) {
            const { selector, inferredSelector } = transformer.scopeSelector(meta, atRule.params, atRule, undefined, inferredSelectorMixin);
            // transform selector in params
            atRule.params = selector;
            // track selector context for nested selector nodes
            transformer.containerInferredSelectorMap.set(atRule, inferredSelector);
        }
    },
    transformLastPass({ ast }) {
        // called with experimentalSelectorInference=true
        // flatten @st-scope after transformation
        const toRemove = [];
        for (const node of ast.nodes) {
            if (isStScopeStatement(node)) {
                flattenScope(node);
                toRemove.push(() => node.replaceWith(node.nodes || []));
            }
        }
        toRemove.forEach((remove) => remove());
    },
});
// API
class StylablePublicApi {
    constructor(stylable) {
        this.stylable = stylable;
    }
    getStScope(rule) {
        return getStScope(rule);
    }
}
exports.StylablePublicApi = StylablePublicApi;
function isStScopeStatement(node) {
    return node.type === 'atrule' && node.name === 'st-scope';
}
exports.isStScopeStatement = isStScopeStatement;
function flattenScope(atRule) {
    const scopeSelector = atRule.params;
    if (scopeSelector) {
        atRule.walkRules((rule) => {
            rule.selector = (0, selector_1.scopeNestedSelector)((0, selector_1.parseSelectorWithCache)(scopeSelector), (0, selector_1.parseSelectorWithCache)(rule.selector)).selector;
            rule.stScopeSelector = atRule.params;
        });
    }
}
function getStScope(rule) {
    let current = rule;
    while (current?.parent) {
        current = current.parent;
        if (isStScopeStatement(current) && current.parent?.type === 'root') {
            return current;
        }
    }
    return;
}
//# sourceMappingURL=st-scope.js.map