"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContentTypesXform = void 0;
const xml_stream_1 = require("../../../utils/xml-stream.js");
const base_xform_1 = require("../base-xform.js");
const ooxml_paths_1 = require("../../../utils/ooxml-paths.js");
// used for rendering the [Content_Types].xml file
// not used for parsing
class ContentTypesXform extends base_xform_1.BaseXform {
    render(xmlStream, model) {
        xmlStream.openXml(xml_stream_1.XmlStream.StdDocAttributes);
        xmlStream.openNode("Types", ContentTypesXform.PROPERTY_ATTRIBUTES);
        const mediaHash = {};
        (model.media ?? []).forEach((medium) => {
            if (medium.type === "image") {
                const imageType = medium.extension;
                if (!mediaHash[imageType]) {
                    mediaHash[imageType] = true;
                    xmlStream.leafNode("Default", {
                        Extension: imageType,
                        ContentType: `image/${imageType}`
                    });
                }
            }
        });
        xmlStream.leafNode("Default", {
            Extension: "rels",
            ContentType: "application/vnd.openxmlformats-package.relationships+xml"
        });
        xmlStream.leafNode("Default", { Extension: "xml", ContentType: "application/xml" });
        xmlStream.leafNode("Override", {
            PartName: (0, ooxml_paths_1.toContentTypesPartName)(ooxml_paths_1.OOXML_PATHS.xlWorkbook),
            ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"
        });
        model.worksheets.forEach((worksheet) => {
            xmlStream.leafNode("Override", {
                PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.worksheetPath)(worksheet.fileIndex)),
                ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"
            });
        });
        if ((model.pivotTables ?? []).length) {
            // R9-B6: Deduplicate cache content types by cacheId. When multiple pivot tables
            // share the same cache, the cache definition/records files are written only once.
            const writtenCacheIds = new Set();
            // Add content types for each pivot table
            (model.pivotTables ?? []).forEach((pivotTable) => {
                const n = pivotTable.tableNumber;
                const cacheId = pivotTable.cacheId;
                if (!writtenCacheIds.has(cacheId)) {
                    writtenCacheIds.add(cacheId);
                    xmlStream.leafNode("Override", {
                        PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.pivotCacheDefinitionPath)(n)),
                        ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheDefinition+xml"
                    });
                    // R9-B5: Only register cacheRecords content type when the file actually exists.
                    // Loaded pivot tables may lack cacheRecords (e.g. OLAP sources).
                    const hasCacheRecords = pivotTable.isLoaded ? !!pivotTable.cacheRecords : true;
                    if (hasCacheRecords) {
                        xmlStream.leafNode("Override", {
                            PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.pivotCacheRecordsPath)(n)),
                            ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotCacheRecords+xml"
                        });
                    }
                }
                // Each pivot table always has its own file
                xmlStream.leafNode("Override", {
                    PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.pivotTablePath)(n)),
                    ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.pivotTable+xml"
                });
            });
        }
        xmlStream.leafNode("Override", {
            PartName: (0, ooxml_paths_1.toContentTypesPartName)(ooxml_paths_1.OOXML_PATHS.xlTheme1),
            ContentType: "application/vnd.openxmlformats-officedocument.theme+xml"
        });
        xmlStream.leafNode("Override", {
            PartName: (0, ooxml_paths_1.toContentTypesPartName)(ooxml_paths_1.OOXML_PATHS.xlStyles),
            ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml"
        });
        // Add FeaturePropertyBag if checkboxes are used
        if (model.hasCheckboxes) {
            xmlStream.leafNode("Override", {
                PartName: (0, ooxml_paths_1.toContentTypesPartName)(ooxml_paths_1.OOXML_PATHS.xlFeaturePropertyBag),
                ContentType: "application/vnd.ms-excel.featurepropertybag+xml"
            });
        }
        const hasSharedStrings = model.sharedStrings && model.sharedStrings.count;
        if (hasSharedStrings) {
            xmlStream.leafNode("Override", {
                PartName: (0, ooxml_paths_1.toContentTypesPartName)(ooxml_paths_1.OOXML_PATHS.xlSharedStrings),
                ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml"
            });
        }
        if (model.tables) {
            model.tables.forEach((table) => {
                xmlStream.leafNode("Override", {
                    PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.tablePath)(table.target)),
                    ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml"
                });
            });
        }
        if (model.drawings) {
            model.drawings.forEach((drawing) => {
                xmlStream.leafNode("Override", {
                    PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.drawingPath)(drawing.name)),
                    ContentType: "application/vnd.openxmlformats-officedocument.drawing+xml"
                });
            });
        }
        // VML extension is needed for comments or form controls
        const hasComments = model.commentRefs && model.commentRefs.length > 0;
        const hasFormControls = model.formControlRefs && model.formControlRefs.length > 0;
        if (hasComments || hasFormControls) {
            xmlStream.leafNode("Default", {
                Extension: "vml",
                ContentType: "application/vnd.openxmlformats-officedocument.vmlDrawing"
            });
        }
        if (hasComments) {
            model.commentRefs.forEach(({ commentName }) => {
                xmlStream.leafNode("Override", {
                    PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.commentsPathFromName)(commentName)),
                    ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml"
                });
            });
        }
        if (hasFormControls) {
            for (const ctrlPropId of model.formControlRefs) {
                xmlStream.leafNode("Override", {
                    PartName: (0, ooxml_paths_1.toContentTypesPartName)((0, ooxml_paths_1.ctrlPropPath)(ctrlPropId)),
                    ContentType: "application/vnd.ms-excel.controlproperties+xml"
                });
            }
        }
        // Add passthrough content types (charts, etc.)
        if (model.passthroughContentTypes) {
            for (const { partName, contentType } of model.passthroughContentTypes) {
                xmlStream.leafNode("Override", {
                    PartName: (0, ooxml_paths_1.toContentTypesPartName)(partName),
                    ContentType: contentType
                });
            }
        }
        xmlStream.leafNode("Override", {
            PartName: (0, ooxml_paths_1.toContentTypesPartName)(ooxml_paths_1.OOXML_PATHS.docPropsCore),
            ContentType: "application/vnd.openxmlformats-package.core-properties+xml"
        });
        xmlStream.leafNode("Override", {
            PartName: (0, ooxml_paths_1.toContentTypesPartName)(ooxml_paths_1.OOXML_PATHS.docPropsApp),
            ContentType: "application/vnd.openxmlformats-officedocument.extended-properties+xml"
        });
        xmlStream.closeNode();
    }
    parseOpen() {
        return false;
    }
    parseText() { }
    parseClose() {
        return false;
    }
}
exports.ContentTypesXform = ContentTypesXform;
ContentTypesXform.PROPERTY_ATTRIBUTES = {
    xmlns: "http://schemas.openxmlformats.org/package/2006/content-types"
};
