{"version":3,"file":"template.mjs","names":[],"sources":["../../../../src/modules/processor/functions/template.ts"],"sourcesContent":["import type {\n    Expression,\n    Program,\n    TemplateElement,\n    TemplateLiteral,\n} from \"oxc-parser\";\n\nimport type { CompilerContext } from \"#/contexts/compiler\";\n\nimport { findInlineExpression } from \"#/ast/expr\";\nimport { CompileError } from \"#/errors/compile\";\n\ntype ExpressionToStringOptions = {\n    context: CompilerContext;\n    program: Program;\n    expression: Expression;\n};\n\nconst expressionToString = (options: ExpressionToStringOptions): string => {\n    const expr: Expression = options.expression;\n\n    // to string\n    if (expr.type === \"Literal\") {\n        if (expr.value === null) return \"\";\n        return expr.value.toString();\n    }\n    // find inline expression\n    else if (expr.type === \"Identifier\") {\n        const inlineExpr: Expression | undefined = findInlineExpression({\n            context: options.context,\n            program: options.program,\n            name: expr.name,\n        });\n\n        if (!inlineExpr) {\n            throw new CompileError({\n                context: options.context,\n                span: {\n                    start: expr.start,\n                    end: expr.end,\n                },\n                message: `Inline expression not found: ${expr.name}`,\n            });\n        }\n\n        const result: string = expressionToString({\n            context: options.context,\n            program: options.program,\n            expression: inlineExpr,\n        });\n\n        return result;\n    }\n    // as const\n    else if (expr.type === \"TSAsExpression\") {\n        return expressionToString({\n            context: options.context,\n            program: options.program,\n            expression: expr.expression,\n        });\n    }\n    // unsupported\n    else {\n        throw new CompileError({\n            context: options.context,\n            span: {\n                start: expr.start,\n                end: expr.end,\n            },\n            message: `Unsupported expression type: ${expr.type}`,\n        });\n    }\n};\n\ntype CollectTemplateLiteralOptions = {\n    context: CompilerContext;\n    program: Program;\n    template: TemplateLiteral;\n};\n\ntype CollectTemplateLiteralResult = {\n    str: string;\n};\n\nconst collectTemplateLiteral = (\n    options: CollectTemplateLiteralOptions,\n): CollectTemplateLiteralResult => {\n    let str: string = \"\";\n\n    const template: TemplateLiteral = options.template;\n\n    for (let i: number = 0; i < template.quasis.length; i++) {\n        // append quasi\n\n        const quasi: TemplateElement | undefined = template.quasis[i];\n\n        if (!quasi) continue;\n\n        str += quasi.value.cooked ?? quasi.value.raw;\n\n        // append expression\n\n        const expression: Expression | undefined = template.expressions[i];\n\n        if (!expression) continue;\n\n        str += expressionToString({\n            context: options.context,\n            program: options.program,\n            expression,\n        });\n    }\n\n    return {\n        str,\n    };\n};\n\nexport type { CollectTemplateLiteralOptions, CollectTemplateLiteralResult };\nexport { collectTemplateLiteral };\n"],"mappings":";;;AAkBA,MAAM,sBAAsB,YAA+C;CACvE,MAAM,OAAmB,QAAQ;CAGjC,IAAI,KAAK,SAAS,WAAW;EACzB,IAAI,KAAK,UAAU,MAAM,OAAO;EAChC,OAAO,KAAK,MAAM,SAAS;CAC/B,OAEK,IAAI,KAAK,SAAS,cAAc;EACjC,MAAM,aAAqC,qBAAqB;GAC5D,SAAS,QAAQ;GACjB,SAAS,QAAQ;GACjB,MAAM,KAAK;EACf,CAAC;EAED,IAAI,CAAC,YACD,MAAM,IAAI,aAAa;GACnB,SAAS,QAAQ;GACjB,MAAM;IACF,OAAO,KAAK;IACZ,KAAK,KAAK;GACd;GACA,SAAS,gCAAgC,KAAK;EAClD,CAAC;EASL,OANuB,mBAAmB;GACtC,SAAS,QAAQ;GACjB,SAAS,QAAQ;GACjB,YAAY;EAChB,CAEY;CAChB,OAEK,IAAI,KAAK,SAAS,kBACnB,OAAO,mBAAmB;EACtB,SAAS,QAAQ;EACjB,SAAS,QAAQ;EACjB,YAAY,KAAK;CACrB,CAAC;MAID,MAAM,IAAI,aAAa;EACnB,SAAS,QAAQ;EACjB,MAAM;GACF,OAAO,KAAK;GACZ,KAAK,KAAK;EACd;EACA,SAAS,gCAAgC,KAAK;CAClD,CAAC;AAET;AAYA,MAAM,0BACF,YAC+B;CAC/B,IAAI,MAAc;CAElB,MAAM,WAA4B,QAAQ;CAE1C,KAAK,IAAI,IAAY,GAAG,IAAI,SAAS,OAAO,QAAQ,KAAK;EAGrD,MAAM,QAAqC,SAAS,OAAO;EAE3D,IAAI,CAAC,OAAO;EAEZ,OAAO,MAAM,MAAM,UAAU,MAAM,MAAM;EAIzC,MAAM,aAAqC,SAAS,YAAY;EAEhE,IAAI,CAAC,YAAY;EAEjB,OAAO,mBAAmB;GACtB,SAAS,QAAQ;GACjB,SAAS,QAAQ;GACjB;EACJ,CAAC;CACL;CAEA,OAAO,EACH,IACJ;AACJ"}