import { buildCellNumberFormatCode, getCellNumberFormatKind, } from '@bilig/protocol';
export function deleteRecordsBySheet(bucket, sheetName, readSheetName) {
    for (const [key, record] of bucket.entries()) {
        if (readSheetName(record) === sheetName) {
            bucket.delete(key);
        }
    }
}
export function normalizeCellStyleRecord(style) {
    const record = { id: style.id.trim() };
    if (record.id.length === 0) {
        throw new Error('Cell style id must be non-empty');
    }
    const fillColor = normalizeBackgroundColor(style.fill?.backgroundColor);
    if (fillColor) {
        record.fill = { backgroundColor: fillColor };
    }
    const fontFamily = normalizeFontFamily(style.font?.family);
    const fontColor = normalizeColor(style.font?.color);
    const fontSize = typeof style.font?.size === 'number' && Number.isFinite(style.font.size) ? Math.max(1, Math.min(144, style.font.size)) : undefined;
    if (fontFamily ||
        fontColor ||
        fontSize !== undefined ||
        style.font?.bold === true ||
        style.font?.italic === true ||
        style.font?.underline === true) {
        record.font = {
            ...(fontFamily ? { family: fontFamily } : {}),
            ...(fontColor ? { color: fontColor } : {}),
            ...(fontSize !== undefined ? { size: fontSize } : {}),
            ...(style.font?.bold ? { bold: true } : {}),
            ...(style.font?.italic ? { italic: true } : {}),
            ...(style.font?.underline ? { underline: true } : {}),
        };
    }
    const horizontal = normalizeHorizontalAlignment(style.alignment?.horizontal);
    const vertical = normalizeVerticalAlignment(style.alignment?.vertical);
    const wrap = style.alignment?.wrap === true ? true : undefined;
    const indent = typeof style.alignment?.indent === 'number' && Number.isFinite(style.alignment.indent)
        ? Math.max(0, Math.min(16, Math.trunc(style.alignment.indent)))
        : undefined;
    const shrinkToFit = style.alignment?.shrinkToFit === true ? true : undefined;
    const readingOrder = normalizeReadingOrder(style.alignment?.readingOrder);
    const textRotation = normalizeTextRotation(style.alignment?.textRotation);
    const justifyLastLine = style.alignment?.justifyLastLine === true ? true : undefined;
    if (horizontal ||
        vertical ||
        wrap ||
        indent !== undefined ||
        shrinkToFit ||
        readingOrder !== undefined ||
        textRotation !== undefined ||
        justifyLastLine) {
        record.alignment = {
            ...(horizontal ? { horizontal } : {}),
            ...(vertical ? { vertical } : {}),
            ...(wrap ? { wrap: true } : {}),
            ...(indent !== undefined ? { indent } : {}),
            ...(shrinkToFit ? { shrinkToFit: true } : {}),
            ...(readingOrder !== undefined ? { readingOrder } : {}),
            ...(textRotation !== undefined ? { textRotation } : {}),
            ...(justifyLastLine ? { justifyLastLine: true } : {}),
        };
    }
    const borders = normalizeBorders(style.borders);
    if (borders) {
        record.borders = borders;
    }
    if (style.protection !== undefined) {
        record.protection = {
            ...(style.protection.locked !== undefined ? { locked: style.protection.locked } : {}),
            ...(style.protection.hidden !== undefined ? { hidden: style.protection.hidden } : {}),
        };
    }
    return record;
}
export function normalizeCellNumberFormatRecord(format) {
    const id = format.id.trim();
    if (id.length === 0) {
        throw new Error('Cell number format id must be non-empty');
    }
    const code = buildCellNumberFormatCode(format.code);
    return {
        id,
        code,
        kind: format.kind ?? getCellNumberFormatKind(code),
    };
}
export function cellStyleKey(style) {
    return JSON.stringify({
        fill: style.fill?.backgroundColor ?? null,
        font: style.font ?? null,
        alignment: style.alignment ?? null,
        borders: style.borders ?? null,
        protection: style.protection ?? null,
    });
}
export function cellStyleIdForKey(key) {
    let hash = 2166136261;
    for (let index = 0; index < key.length; index += 1) {
        hash ^= key.charCodeAt(index);
        hash = Math.imul(hash, 16777619);
    }
    return `style-${(hash >>> 0).toString(16)}`;
}
export function cellNumberFormatIdForCode(code) {
    let hash = 2166136261;
    for (let index = 0; index < code.length; index += 1) {
        hash ^= code.charCodeAt(index);
        hash = Math.imul(hash, 16777619);
    }
    return `format-${(hash >>> 0).toString(16)}`;
}
export function axisMetadataKey(sheetName, start, count) {
    return `${sheetName}:${start}:${count}`;
}
function normalizeBackgroundColor(color) {
    return normalizeColor(color);
}
function normalizeColor(color) {
    if (!color) {
        return undefined;
    }
    const trimmed = color.trim().toLowerCase();
    if (/^#[0-9a-f]{6}$/.test(trimmed)) {
        return trimmed;
    }
    if (/^#[0-9a-f]{3}$/.test(trimmed)) {
        return `#${trimmed[1]}${trimmed[1]}${trimmed[2]}${trimmed[2]}${trimmed[3]}${trimmed[3]}`;
    }
    throw new Error(`Unsupported background color: ${color}`);
}
function normalizeFontFamily(family) {
    if (!family) {
        return undefined;
    }
    const trimmed = family.trim();
    return trimmed.length === 0 ? undefined : trimmed;
}
function normalizeHorizontalAlignment(value) {
    switch (value) {
        case undefined:
            return undefined;
        case 'general':
        case 'left':
        case 'center':
        case 'right':
        case 'fill':
        case 'justify':
        case 'centerContinuous':
        case 'distributed':
            return value;
        default:
            return undefined;
    }
}
function normalizeVerticalAlignment(value) {
    switch (value) {
        case undefined:
            return undefined;
        case 'top':
        case 'middle':
        case 'bottom':
        case 'justify':
        case 'distributed':
            return value;
        default:
            return undefined;
    }
}
function normalizeReadingOrder(value) {
    if (typeof value !== 'number' || !Number.isFinite(value)) {
        return undefined;
    }
    const normalized = Math.trunc(value);
    return normalized >= 0 && normalized <= 2 ? normalized : undefined;
}
function normalizeTextRotation(value) {
    if (typeof value !== 'number' || !Number.isFinite(value)) {
        return undefined;
    }
    const normalized = Math.trunc(value);
    if (normalized === 255) {
        return normalized;
    }
    return normalized >= 0 && normalized <= 180 ? normalized : undefined;
}
function normalizeBorderStyle(value) {
    switch (value) {
        case undefined:
            return undefined;
        case 'solid':
        case 'dashed':
        case 'dotted':
        case 'double':
            return value;
        default:
            return undefined;
    }
}
function normalizeBorderWeight(value) {
    switch (value) {
        case undefined:
            return undefined;
        case 'thin':
        case 'medium':
        case 'thick':
            return value;
        default:
            return undefined;
    }
}
function normalizeBorderSide(side) {
    if (!side) {
        return undefined;
    }
    const color = normalizeColor(side.color);
    const style = normalizeBorderStyle(side.style);
    const weight = normalizeBorderWeight(side.weight);
    if (!color || !style || !weight) {
        return undefined;
    }
    return { color, style, weight };
}
function normalizeBorders(style) {
    if (!style) {
        return undefined;
    }
    const top = normalizeBorderSide(style.top);
    const right = normalizeBorderSide(style.right);
    const bottom = normalizeBorderSide(style.bottom);
    const left = normalizeBorderSide(style.left);
    if (!top && !right && !bottom && !left) {
        return undefined;
    }
    return {
        ...(top ? { top } : {}),
        ...(right ? { right } : {}),
        ...(bottom ? { bottom } : {}),
        ...(left ? { left } : {}),
    };
}
//# sourceMappingURL=workbook-store-records.js.map