"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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;
};
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.configForComponent = void 0;
const postcss_1 = __importDefault(require("postcss"));
const fs_1 = __importDefault(require("fs"));
const debug_1 = __importDefault(require("debug"));
const deepmerge_1 = __importDefault(require("deepmerge"));
const caniuse = __importStar(require("caniuse-api"));
const browserslist_1 = __importDefault(require("browserslist"));
const tsNode = __importStar(require("ts-node"));
const common_1 = require("./common");
const modern_1 = require("./modern");
const legacy_1 = require("./legacy");
const log = debug_1.default('postcss-themed');
tsNode.register({
    compilerOptions: { module: 'commonjs' },
    transpileOnly: true,
});
/** Try to load component theme from same directory as css file */
const configForComponent = (cssFile, rootTheme, resolveTheme) => {
    if (!cssFile) {
        return {};
    }
    try {
        let componentConfig;
        if (resolveTheme) {
            componentConfig = resolveTheme(cssFile);
        }
        else {
            const theme = common_1.getThemeFilename(cssFile);
            delete require.cache[require.resolve(theme)];
            // eslint-disable-next-line security/detect-non-literal-require, global-require
            componentConfig = require(theme);
        }
        const fn = 'default' in componentConfig ? componentConfig.default : componentConfig;
        return fn(rootTheme);
    }
    catch (error) {
        if (error instanceof SyntaxError || error instanceof TypeError) {
            throw error;
        }
        else {
            log(error);
        }
        return {};
    }
};
exports.configForComponent = configForComponent;
/** Generate a theme */
const themeFile = (options = {}) => (root, result) => {
    // Postcss-modules runs twice and we only ever want to process the CSS once
    // @ts-ignore
    if (root.source.processed) {
        return;
    }
    const { config, resolveTheme } = options;
    if (!config) {
        throw Error('No config provided to postcss-themed');
    }
    if (!root.source) {
        throw Error('No source found');
    }
    const globalConfig = common_1.normalizeTheme(config);
    const componentConfig = common_1.normalizeTheme(exports.configForComponent(root.source.input.file, config, resolveTheme));
    const mergedConfig = deepmerge_1.default(globalConfig, componentConfig);
    common_1.resolveThemeExtension(mergedConfig);
    if (caniuse.isSupported('css-variables', browserslist_1.default())) {
        modern_1.modernTheme(root, mergedConfig, options);
    }
    else {
        legacy_1.legacyTheme(root, mergedConfig, options);
    }
    // @ts-ignore
    root.source.processed = true;
    if (!resolveTheme && root.source.input.file) {
        const themeFilename = common_1.getThemeFilename(root.source.input.file);
        if (fs_1.default.existsSync(themeFilename)) {
            result.messages.push({
                plugin: 'postcss-themed',
                type: 'dependency',
                file: themeFilename,
            });
        }
    }
};
__exportStar(require("./types"), exports);
exports.default = postcss_1.default.plugin('postcss-themed', themeFile);
//# sourceMappingURL=index.js.map