"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PageSetupXform = void 0;
const base_xform_1 = require("../base-xform.js");
function booleanToXml(model) {
    return model ? "1" : undefined;
}
function pageOrderToXml(model) {
    switch (model) {
        case "overThenDown":
            return model;
        default:
            return undefined;
    }
}
function cellCommentsToXml(model) {
    switch (model) {
        case "atEnd":
        case "asDisplyed":
            return model;
        default:
            return undefined;
    }
}
function errorsToXml(model) {
    switch (model) {
        case "dash":
        case "blank":
        case "NA":
            return model;
        default:
            return undefined;
    }
}
function pageSizeToModel(value) {
    return value !== undefined ? parseInt(value, 10) : undefined;
}
class PageSetupXform extends base_xform_1.BaseXform {
    get tag() {
        return "pageSetup";
    }
    _dpiToXml(value) {
        // Excel commonly omits these attributes. 4294967295 is used as a sentinel default
        // when parsing missing values; it should never be serialized back out.
        if (value === undefined) {
            return undefined;
        }
        if (!Number.isFinite(value)) {
            return undefined;
        }
        if (value === 4294967295) {
            return undefined;
        }
        return value;
    }
    render(xmlStream, model) {
        if (model) {
            const attributes = {
                paperSize: model.paperSize,
                orientation: model.orientation,
                horizontalDpi: this._dpiToXml(model.horizontalDpi),
                verticalDpi: this._dpiToXml(model.verticalDpi),
                pageOrder: pageOrderToXml(model.pageOrder),
                blackAndWhite: booleanToXml(model.blackAndWhite),
                draft: booleanToXml(model.draft),
                cellComments: cellCommentsToXml(model.cellComments),
                errors: errorsToXml(model.errors),
                // Only output non-default values (matches Excel behavior)
                scale: model.scale !== 100 ? model.scale : undefined,
                fitToWidth: model.fitToWidth !== 1 ? model.fitToWidth : undefined,
                fitToHeight: model.fitToHeight !== 1 ? model.fitToHeight : undefined,
                firstPageNumber: model.firstPageNumber,
                useFirstPageNumber: booleanToXml(!!model.firstPageNumber),
                usePrinterDefaults: booleanToXml(model.usePrinterDefaults),
                copies: model.copies
            };
            if (Object.values(attributes).some((value) => value !== undefined)) {
                xmlStream.leafNode(this.tag, attributes);
            }
        }
    }
    parseOpen(node) {
        switch (node.name) {
            case this.tag:
                this.model = {
                    paperSize: pageSizeToModel(node.attributes.paperSize),
                    orientation: node.attributes.orientation ?? "portrait",
                    horizontalDpi: parseInt(node.attributes.horizontalDpi ?? "4294967295", 10),
                    verticalDpi: parseInt(node.attributes.verticalDpi ?? "4294967295", 10),
                    pageOrder: node.attributes.pageOrder ?? "downThenOver",
                    blackAndWhite: node.attributes.blackAndWhite === "1",
                    draft: node.attributes.draft === "1",
                    cellComments: node.attributes.cellComments ?? "None",
                    errors: node.attributes.errors ?? "displayed",
                    scale: parseInt(node.attributes.scale ?? "100", 10),
                    fitToWidth: parseInt(node.attributes.fitToWidth ?? "1", 10),
                    fitToHeight: parseInt(node.attributes.fitToHeight ?? "1", 10),
                    firstPageNumber: parseInt(node.attributes.firstPageNumber ?? "1", 10),
                    useFirstPageNumber: node.attributes.useFirstPageNumber === "1",
                    usePrinterDefaults: node.attributes.usePrinterDefaults === "1",
                    copies: parseInt(node.attributes.copies ?? "1", 10)
                };
                return true;
            default:
                return false;
        }
    }
    parseText() { }
    parseClose() {
        return false;
    }
}
exports.PageSetupXform = PageSetupXform;
