import { canonicalWorkbookRangeRef } from '../workbook-range-records.js';
export function buildSetSheetProtectionOps(workbook, protection) {
    const existing = workbook.getSheetProtection(protection.sheetName);
    const normalized = {
        sheetName: protection.sheetName,
        ...(protection.hideFormulas !== undefined ? { hideFormulas: protection.hideFormulas } : {}),
        ...(protection.xmlAttributes ? { xmlAttributes: protection.xmlAttributes.map((attribute) => ({ ...attribute })) } : {}),
    };
    if (existing && JSON.stringify(existing) === JSON.stringify(normalized)) {
        return null;
    }
    return [{ kind: 'setSheetProtection', protection: normalized }];
}
export function buildSetFilterOps(workbook, sheetName, range) {
    const existing = workbook.getFilter(sheetName, range);
    if (existing && JSON.stringify(existing.range) === JSON.stringify(range)) {
        return null;
    }
    return [{ kind: 'setFilter', sheetName, range: structuredClone(range) }];
}
export function buildSetSortOps(workbook, sheetName, range, keys) {
    const existing = workbook.getSort(sheetName, range);
    const normalizedKeys = keys.map((key) => Object.assign({}, key));
    if (existing &&
        existing.keys.length === normalizedKeys.length &&
        existing.keys.every((key, index) => key.keyAddress === normalizedKeys[index]?.keyAddress && key.direction === normalizedKeys[index]?.direction)) {
        return null;
    }
    return [{ kind: 'setSort', sheetName, range: { ...range }, keys: normalizedKeys }];
}
export function buildSetDataValidationOps(workbook, validation) {
    const existing = workbook.getDataValidation(validation.range.sheetName, validation.range);
    const normalized = {
        ...structuredClone(validation),
        range: canonicalWorkbookRangeRef(validation.range),
    };
    if (existing && JSON.stringify(existing) === JSON.stringify(normalized)) {
        return null;
    }
    return [{ kind: 'setDataValidation', validation: normalized }];
}
export function buildSetConditionalFormatOps(workbook, format) {
    const normalized = {
        ...structuredClone(format),
        id: format.id.trim(),
        range: canonicalWorkbookRangeRef(format.range),
    };
    const existing = workbook.getConditionalFormat(normalized.id);
    if (existing && JSON.stringify(existing) === JSON.stringify(normalized)) {
        return null;
    }
    return [{ kind: 'upsertConditionalFormat', format: normalized }];
}
export function buildSetRangeProtectionOps(workbook, protection) {
    const normalized = {
        ...structuredClone(protection),
        id: protection.id.trim(),
        range: canonicalWorkbookRangeRef(protection.range),
    };
    const existing = workbook.getRangeProtection(normalized.id);
    if (existing && JSON.stringify(existing) === JSON.stringify(normalized)) {
        return null;
    }
    return [{ kind: 'upsertRangeProtection', protection: normalized }];
}
export function buildSetPivotTableOps(sheetName, address, definition) {
    return [
        {
            kind: 'upsertPivotTable',
            name: definition.name.trim(),
            sheetName,
            address,
            source: { ...definition.source },
            groupBy: [...definition.groupBy],
            values: definition.values.map((value) => Object.assign({}, value)),
            rows: 1,
            cols: Math.max(definition.groupBy.length + definition.values.length, 1),
        },
    ];
}
//# sourceMappingURL=engine-workbook-metadata-ops.js.map