"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTemplatedUrlRowDependencies = exports.renderRichTextTemplate = exports.renderTemplatedUrl = void 0;
const liquidjs_1 = require("liquidjs");
const templateEngine = new liquidjs_1.Liquid({
    cache: true,
    timezoneOffset: 0,
    outputDelimiterLeft: '${',
    outputDelimiterRight: '}',
    ownPropertyOnly: true,
    strictVariables: true,
    strictFilters: true,
});
const renderTemplatedUrl = (templatedUrl, value, row) => templateEngine.parseAndRenderSync(templatedUrl, { value, row });
exports.renderTemplatedUrl = renderTemplatedUrl;
const renderRichTextTemplate = (template, value, row) => templateEngine.parseAndRenderSync(template, { value, row });
exports.renderRichTextTemplate = renderRichTextTemplate;
const getTemplatedUrlRowDependencies = (templatedUrl) => {
    const parts = templateEngine.parse(templatedUrl);
    return parts.reduce((acc, part) => {
        if (part.token.kind === liquidjs_1.TokenKind.Output) {
            const referenceString = part.token.getText();
            const valueReference = referenceString.match(/value\.(raw|formatted)/)?.[0];
            const rowReference = referenceString.match(/row\.([a-z\\._]+)\.([a-z\\._]+)\.(raw|formatted)/)?.[0];
            if (!valueReference && !rowReference) {
                throw new Error(`Found invalid reference "${referenceString}" in your url template`);
            }
            if (!valueReference && rowReference) {
                const referenceParts = rowReference.split('.');
                return [...acc, `${referenceParts[1]}_${referenceParts[2]}`];
            }
        }
        return acc;
    }, []);
};
exports.getTemplatedUrlRowDependencies = getTemplatedUrlRowDependencies;
//# sourceMappingURL=template.js.map