"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateStylableJSModuleSource = void 0;
function generateStylableJSModuleSource(moduleOptions, injectOptions) {
    const { namespace, imports = [], jsExports, moduleType, runtimeRequest, varType = 'const', header = '', footer = '', } = moduleOptions;
    const { classes, keyframes, layers, containers, stVars, vars } = jsExports;
    const exportKind = moduleType === 'esm' ? `export ${varType} ` : 'module.exports.';
    return `
${imports.map(moduleRequest(moduleType)).join('\n')}
${runtimeImport(moduleType, runtimeRequest, injectOptions)}

${header}

${varType} _namespace_ = ${JSON.stringify(namespace)};
${varType} _style_ = (...args) => classesRuntime(_namespace_, ...args);

${exportKind}cssStates = (stateMapping) => statesRuntime(_namespace_, stateMapping);
${exportKind}style = _style_;
${exportKind}st = _style_;

${exportKind}namespace = _namespace_;
${exportKind}classes = ${JSON.stringify(classes)};
${exportKind}keyframes = ${JSON.stringify(keyframes)}; 
${exportKind}layers = ${JSON.stringify(layers)};
${exportKind}containers = ${JSON.stringify(containers)};
${exportKind}stVars = ${JSON.stringify(stVars)}; 
${exportKind}vars = ${JSON.stringify(vars)}; 

${runtimeExecuteInject(moduleType, injectOptions)}

${footer}
`;
}
exports.generateStylableJSModuleSource = generateStylableJSModuleSource;
function moduleRequest(moduleType) {
    return ({ from, defaultImport }) => {
        const request = JSON.stringify(from);
        if (defaultImport) {
            return moduleType === 'esm'
                ? `import ${defaultImport} from ${request};`
                : `const ${defaultImport} = require(${request});`;
        }
        return moduleType === 'esm' ? `import ${request};` : `require(${request});`;
    };
}
function runtimeImport(moduleType, runtimeRequest, injectOptions) {
    const importInjectCSS = injectOptions?.css ? `, injectCSS` : '';
    const request = JSON.stringify(runtimeRequest ?? '@stylable/runtime');
    return moduleType === 'esm'
        ? `import { classesRuntime, statesRuntime${importInjectCSS} } from ${request};`
        : `const { classesRuntime, statesRuntime${importInjectCSS} } = require(${request});`;
}
function runtimeExecuteInject(moduleType, injectOptions) {
    if (!injectOptions?.css) {
        return '';
    }
    const { id, css, cssCode, depthCode, depth, runtimeId } = injectOptions;
    let out = 'injectCSS(';
    out += id ? JSON.stringify(id) : moduleType === 'esm' ? 'import.meta.url' : 'module.id';
    out += ', ';
    out += cssCode || JSON.stringify(css);
    out += ', ';
    out += depthCode || JSON.stringify(depth) || '-1';
    out += ', ';
    out += JSON.stringify(runtimeId);
    out += ');';
    return out;
}
//# sourceMappingURL=stylable-js-module-source.js.map