import { ValueTag } from '@bilig/protocol';
import { translateMutationFormulaForTarget } from './mutation-cell-content-helpers.js';
export function readStoredMutationCellState(snapshot, format, cellFlags, styleId) {
    if (snapshot.formula !== undefined) {
        return { formula: snapshot.formula, value: null, format, ...(styleId !== undefined ? { styleId } : {}) };
    }
    const authoredBlank = ((cellFlags ?? 0) & 256 /* CellFlags.AuthoredBlank */) !== 0;
    switch (snapshot.value.tag) {
        case ValueTag.Number:
        case ValueTag.Boolean:
        case ValueTag.String:
            return { value: snapshot.value.value, format, ...(styleId !== undefined ? { styleId } : {}) };
        case ValueTag.Empty:
        case ValueTag.Error:
            return { value: null, format, authoredBlank, ...(styleId !== undefined ? { styleId } : {}) };
    }
}
export function readDesiredMutationCellState(args) {
    const format = args.formatOverride ?? args.snapshot.format ?? null;
    const styleId = args.styleIdOverride ?? args.snapshot.styleId;
    if (args.snapshot.formula !== undefined) {
        return {
            formula: args.sourceSheetName && args.sourceAddress
                ? translateMutationFormulaForTarget(args.snapshot.formula, args.sourceSheetName, args.sourceAddress, args.targetSheetName, args.targetAddress)
                : args.snapshot.formula,
            value: null,
            format,
            ...(styleId !== undefined ? { styleId } : {}),
        };
    }
    switch (args.snapshot.value.tag) {
        case ValueTag.Number:
        case ValueTag.Boolean:
        case ValueTag.String:
            return { value: args.snapshot.value.value, format, ...(styleId !== undefined ? { styleId } : {}) };
        case ValueTag.Empty:
        case ValueTag.Error:
            return {
                value: null,
                format,
                authoredBlank: (args.snapshot.flags & 256 /* CellFlags.AuthoredBlank */) !== 0,
                ...(styleId !== undefined ? { styleId } : {}),
            };
    }
}
export function hasMutationCellContent(snapshot) {
    if (snapshot.formula !== undefined) {
        return true;
    }
    return snapshot.value.tag !== ValueTag.Empty || (snapshot.flags & 256 /* CellFlags.AuthoredBlank */) !== 0;
}
export function hasStoredMutationCellState(snapshot, format, cellFlags) {
    return (snapshot.formula !== undefined ||
        snapshot.value.tag !== ValueTag.Empty ||
        format !== undefined ||
        ((cellFlags ?? 0) & 256 /* CellFlags.AuthoredBlank */) !== 0);
}
export function shouldApplyMutationCellState(current, desired) {
    if (desired.formula === undefined &&
        desired.value === null &&
        desired.format === null &&
        desired.authoredBlank === true &&
        current.formula === undefined &&
        current.value === null &&
        current.format === null &&
        !(current.authoredBlank ?? false)) {
        return false;
    }
    return (current.formula !== desired.formula ||
        current.value !== desired.value ||
        current.format !== desired.format ||
        current.styleId !== desired.styleId ||
        (current.authoredBlank ?? false) !== (desired.authoredBlank ?? false));
}
//# sourceMappingURL=mutation-cell-state-helpers.js.map