import { rewriteAddressForStructuralTransform, rewriteFormulaForStructuralTransform, rewriteRangeForStructuralTransform, } from '@bilig/formula';
function decodeXmlText(value) {
    return value
        .replace(/&quot;/gu, '"')
        .replace(/&apos;/gu, "'")
        .replace(/&lt;/gu, '<')
        .replace(/&gt;/gu, '>')
        .replace(/&amp;/gu, '&');
}
function escapeXmlText(value) {
    return value.replace(/&/gu, '&amp;').replace(/</gu, '&lt;').replace(/>/gu, '&gt;');
}
function rewriteSparklineReference(value, transform) {
    const [start, end, extra] = value.split(':');
    if (!start || extra !== undefined) {
        return undefined;
    }
    try {
        if (!end) {
            return rewriteAddressForStructuralTransform(start, transform);
        }
        const rewritten = rewriteRangeForStructuralTransform(start, end, transform);
        return rewritten ? `${rewritten.startAddress}:${rewritten.endAddress}` : undefined;
    }
    catch {
        return undefined;
    }
}
function rewriteSparklineSqref(value, transform) {
    const refs = value.trim().split(/\s+/u).filter(Boolean);
    if (refs.length === 0) {
        return undefined;
    }
    const rewritten = refs.flatMap((ref) => {
        const nextRef = rewriteSparklineReference(ref, transform);
        return nextRef ? [nextRef] : [];
    });
    return rewritten.length > 0 ? rewritten.join(' ') : undefined;
}
function rewriteSparklineElementText(xml, elementName, rewrite) {
    let failed = false;
    const pattern = new RegExp(`(<(?:[A-Za-z_][\\w.-]*:)?${elementName}\\b[^>]*>)([\\s\\S]*?)(</(?:[A-Za-z_][\\w.-]*:)?${elementName}>)`, 'gu');
    const nextXml = xml.replace(pattern, (match, open, text, close) => {
        const nextText = rewrite(decodeXmlText(text));
        if (nextText === undefined) {
            failed = true;
            return match;
        }
        return `${open}${escapeXmlText(nextText)}${close}`;
    });
    return { xml: nextXml, failed };
}
export function rewriteSparklinesForStructuralTransform(sheetName, sparklines, transform) {
    if (!sparklines) {
        return undefined;
    }
    const withFormulas = rewriteSparklineFormulasXml(sparklines.xml, sheetName, sheetName, transform);
    if (!withFormulas) {
        return undefined;
    }
    const withSqrefs = rewriteSparklineElementText(withFormulas, 'sqref', (sqref) => rewriteSparklineSqref(sqref, transform));
    return withSqrefs.failed ? undefined : { xml: withSqrefs.xml };
}
export function rewriteSparklineFormulaRefsForStructuralTransform(ownerSheetName, sparklines, targetSheetName, transform) {
    if (!sparklines) {
        return undefined;
    }
    const xml = rewriteSparklineFormulasXml(sparklines.xml, ownerSheetName, targetSheetName, transform);
    return xml ? { xml } : undefined;
}
function rewriteSparklineFormulasXml(xml, ownerSheetName, targetSheetName, transform) {
    const withFormulas = rewriteSparklineElementText(xml, 'f', (formula) => {
        try {
            return rewriteFormulaForStructuralTransform(formula, ownerSheetName, targetSheetName, transform);
        }
        catch {
            return undefined;
        }
    });
    return withFormulas.failed ? undefined : withFormulas.xml;
}
//# sourceMappingURL=structure-sparkline-metadata-rewrite.js.map