"use strict";
/**
 * PassthroughManager - Manages passthrough files for round-trip preservation
 *
 * This module handles files that are not fully parsed by the library but need to be
 * preserved during read/write cycles (e.g., charts, sparklines, slicers).
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.PassthroughManager = void 0;
// Pre-compiled regex patterns for content type detection (performance optimization)
const chartXmlRegex = /^xl\/charts\/chart\d+\.xml$/;
const chartStyleXmlRegex = /^xl\/charts\/style\d+\.xml$/;
const chartColorsXmlRegex = /^xl\/charts\/colors\d+\.xml$/;
/**
 * Content type definitions for passthrough files
 */
const PASSTHROUGH_CONTENT_TYPES = new Map([
    [chartXmlRegex, "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"],
    [chartStyleXmlRegex, "application/vnd.ms-office.chartstyle+xml"],
    [chartColorsXmlRegex, "application/vnd.ms-office.chartcolorstyle+xml"]
]);
/**
 * Passthrough path prefixes that should be preserved
 */
const PASSTHROUGH_PREFIXES = ["xl/charts/"];
/**
 * PassthroughManager handles storage and retrieval of passthrough files
 * that need to be preserved during Excel read/write cycles.
 */
class PassthroughManager {
    constructor() {
        this.files = new Map();
    }
    /**
     * Check if a path should be treated as passthrough
     */
    static isPassthroughPath(path) {
        return PASSTHROUGH_PREFIXES.some(prefix => path.startsWith(prefix));
    }
    /**
     * Get the content type for a passthrough file path
     * @returns Content type string or undefined if unknown
     */
    static getContentType(path) {
        // Chart relationships are handled by Default extension="rels"
        if (path.startsWith("xl/charts/_rels/")) {
            return undefined;
        }
        for (const [regex, contentType] of PASSTHROUGH_CONTENT_TYPES) {
            if (regex.test(path)) {
                return contentType;
            }
        }
        return undefined;
    }
    /**
     * Add a file to passthrough storage
     */
    add(path, data) {
        this.files.set(path, data);
    }
    /**
     * Get a file from passthrough storage
     */
    get(path) {
        return this.files.get(path);
    }
    /**
     * Check if a file exists in passthrough storage
     */
    has(path) {
        return this.files.has(path);
    }
    /**
     * Get all stored paths
     */
    getPaths() {
        return [...this.files.keys()];
    }
    /**
     * Get all files as a record (for serialization)
     */
    toRecord() {
        const record = {};
        for (const [path, data] of this.files) {
            record[path] = data;
        }
        return record;
    }
    /**
     * Load files from a record (for deserialization)
     */
    fromRecord(record) {
        this.files.clear();
        for (const [path, data] of Object.entries(record)) {
            this.files.set(path, data);
        }
    }
    /**
     * Get content types for all stored files that have known types
     */
    getContentTypes() {
        const contentTypes = [];
        for (const path of this.files.keys()) {
            const contentType = PassthroughManager.getContentType(path);
            if (contentType) {
                contentTypes.push({ partName: path, contentType });
            }
        }
        return contentTypes;
    }
    /**
     * Write all passthrough files to a ZIP writer
     */
    writeToZip(zip) {
        for (const [path, data] of this.files) {
            zip.append(data, { name: path });
        }
    }
    /**
     * Clear all stored files
     */
    clear() {
        this.files.clear();
    }
    /**
     * Get the number of stored files
     */
    get size() {
        return this.files.size;
    }
}
exports.PassthroughManager = PassthroughManager;
