import { logFieldValueDiagnostic, summarizeField, summarizeFieldValue, } from "../fieldValueDiagnostics";
export function isBridgePatchableTextField(field, isRichText) {
    const fieldType = field.type.toLowerCase();
    return (isRichText === true ||
        fieldType === "rich text" ||
        fieldType === "single-line text" ||
        fieldType === "multi-line text" ||
        fieldType === "memo");
}
export function getBridgeFieldPatchValue(field) {
    if (field.rawValue !== undefined && field.rawValue !== null) {
        return String(field.rawValue);
    }
    if (field.value === undefined || field.value === null)
        return "";
    return typeof field.value === "string" ? field.value : String(field.value);
}
export function buildBridgeFieldPatchPayload({ field, structureField, display, activeInlineEdit, }) {
    if (!structureField.elementKey) {
        logFieldValueDiagnostic("bridge-patch", "skipped patch", {
            reason: "missing element key",
            field: summarizeField(field),
            structureField: summarizeStructureField(structureField),
            displayValue: summarizeFieldValue(display.value),
            displaySource: display.source,
        });
        return undefined;
    }
    if (!isBridgePatchableTextField(field, structureField.isRichText)) {
        logFieldValueDiagnostic("bridge-patch", "skipped patch", {
            reason: "field not patchable",
            field: summarizeField(field),
            structureField: summarizeStructureField(structureField),
            displayValue: summarizeFieldValue(display.value),
            displaySource: display.source,
        });
        return undefined;
    }
    if (structureField.elementKey === activeInlineEdit?.elementKey ||
        activeInlineEditMatchesField(activeInlineEdit, structureField)) {
        logFieldValueDiagnostic("bridge-patch", "skipped patch", {
            reason: "active inline edit",
            field: summarizeField(field),
            structureField: summarizeStructureField(structureField),
            activeInlineEdit: activeInlineEdit
                ? {
                    ...activeInlineEdit,
                    value: summarizeFieldValue(activeInlineEdit.value),
                }
                : undefined,
            displayValue: summarizeFieldValue(display.value),
            displaySource: display.source,
        });
        return undefined;
    }
    const patch = {
        elementKey: structureField.elementKey,
        fieldId: structureField.fieldId,
        isRichText: structureField.isRichText || field.type.toLowerCase() === "rich text",
        source: display.source,
        value: display.value,
    };
    logFieldValueDiagnostic("bridge-patch", "prepared patch", {
        field: summarizeField(field),
        structureField: summarizeStructureField(structureField),
        patch: {
            ...patch,
            value: summarizeFieldValue(patch.value),
        },
    });
    return patch;
}
function summarizeStructureField(structureField) {
    return {
        elementKey: structureField.elementKey,
        fieldId: structureField.fieldId,
        isRichText: structureField.isRichText,
        item: structureField.item,
        textContent: summarizeFieldValue(structureField.textContent),
    };
}
function activeInlineEditMatchesField(activeInlineEdit, structureField) {
    return !!(activeInlineEdit?.fieldId &&
        bridgeIdsMatch(activeInlineEdit.fieldId, structureField.fieldId) &&
        bridgeItemsMatch(activeInlineEdit.item, structureField.item));
}
function bridgeItemsMatch(left, right) {
    if (!left || !right)
        return false;
    return (bridgeIdsMatch(left.id, right.id) &&
        normalizeBridgeValue(left.language) ===
            normalizeBridgeValue(right.language) &&
        left.version === right.version);
}
function bridgeIdsMatch(left, right) {
    return normalizeBridgeId(left) === normalizeBridgeId(right);
}
function normalizeBridgeId(value) {
    return normalizeBridgeValue(value).replace(/[{}]/g, "");
}
function normalizeBridgeValue(value) {
    return (value ?? "").trim().toLowerCase();
}
//# sourceMappingURL=bridgeFieldPatch.js.map