import { preservedSheetMetadataKeys, preservedWorkbookMetadataKeys, } from './workbook-metadata-types.js';
function isCloneRecord(value) {
    return typeof value === 'object' && value !== null;
}
function clonePreservedMetadataValue(value) {
    if (!isCloneRecord(value)) {
        return value;
    }
    if (Array.isArray(value)) {
        return value.map(clonePreservedMetadataValue);
    }
    if (ArrayBuffer.isView(value)) {
        return structuredClone(value);
    }
    const path = value['path'];
    const storage = value['storage'];
    const dataBase64 = value['dataBase64'];
    const byteLength = value['byteLength'];
    if (typeof path === 'string' && storage === 'base64' && typeof dataBase64 === 'string' && typeof byteLength === 'number') {
        return { path, storage: 'base64', dataBase64, byteLength };
    }
    const xml = value['xml'];
    if (typeof path === 'string' && typeof xml === 'string' && typeof value['readXml'] === 'function') {
        return { path, xml };
    }
    const cloned = {};
    for (const key of Object.keys(value)) {
        const child = value[key];
        if (typeof child !== 'function') {
            cloned[key] = clonePreservedMetadataValue(child);
        }
    }
    return cloned;
}
export function clonePreservedWorkbookMetadata(metadata) {
    const cloned = {};
    for (const key of preservedWorkbookMetadataKeys) {
        const value = metadata?.[key];
        if (value !== undefined) {
            Object.assign(cloned, { [key]: clonePreservedMetadataValue(value) });
        }
    }
    return cloned;
}
export function clonePreservedSheetMetadata(metadata) {
    const cloned = {};
    for (const key of preservedSheetMetadataKeys) {
        const value = metadata?.[key];
        if (value !== undefined) {
            Object.assign(cloned, { [key]: clonePreservedMetadataValue(value) });
        }
    }
    return cloned;
}
export function pickPreservedWorkbookMetadata(metadata) {
    return clonePreservedWorkbookMetadata(metadata);
}
export function pickPreservedSheetMetadata(metadata) {
    return clonePreservedSheetMetadata(metadata);
}
export function hasPreservedWorkbookMetadata(metadata) {
    return preservedWorkbookMetadataKeys.some((key) => metadata?.[key] !== undefined);
}
export function hasPreservedSheetMetadata(metadata) {
    return preservedSheetMetadataKeys.some((key) => metadata?.[key] !== undefined);
}
//# sourceMappingURL=workbook-preserved-metadata.js.map