import { getCommentScrollIntoViewTarget, getSuggestedEditScrollIntoViewTarget, } from "./feedbackScrollTarget";
function normalizeBridgeId(value) {
    return (value ?? "").replace(/[{}-]/g, "").toLowerCase();
}
function bridgeIdsMatch(left, right) {
    const normalizedLeft = normalizeBridgeId(left);
    const normalizedRight = normalizeBridgeId(right);
    return (!!normalizedLeft && !!normalizedRight && normalizedLeft === normalizedRight);
}
export async function selectFeedbackComment(comment, editContext, fieldsContext) {
    const currentPageId = editContext?.currentItemDescriptor?.id;
    if (currentPageId &&
        comment.mainItemId &&
        comment.mainItemId !== currentPageId) {
        await editContext?.loadItem({
            id: comment.mainItemId,
            language: comment.language,
            version: comment.version,
        });
    }
    editContext?.setSelectedComment(comment);
    editContext?.setSelectedSuggestedEdit(undefined);
    editContext?.setScrollIntoView(getCommentScrollIntoViewTarget(comment));
    if (!editContext?.isMobile) {
        editContext?.select([comment.itemId]);
        if (comment.fieldId) {
            fieldsContext?.setFocusedField({
                fieldId: comment.fieldId,
                item: {
                    id: comment.itemId,
                    language: comment.language,
                    version: comment.version,
                },
            }, false);
            const geometry = editContext?.getActiveSlotContext?.()?.primaryPageViewContext
                ?.bridgeGeometry;
            const fieldIsRendered = geometry?.targets.some((target) => {
                if (target.kind !== "field")
                    return false;
                if (!bridgeIdsMatch(target.fieldId, comment.fieldId))
                    return false;
                if (!bridgeIdsMatch(target.item?.id, comment.itemId))
                    return false;
                if (target.item?.language &&
                    comment.language &&
                    target.item.language !== comment.language) {
                    return false;
                }
                if (typeof target.item?.version === "number" &&
                    typeof comment.version === "number" &&
                    target.item.version !== comment.version) {
                    return false;
                }
                return true;
            });
            if (!fieldIsRendered) {
                const slotId = editContext?.activeSlotId ?? null;
                if (editContext?.isEditorFormHiddenForSlot(slotId)) {
                    editContext.setEditorFormHiddenForSlot(slotId, false);
                }
            }
        }
    }
}
export function selectFeedbackSuggestedEdit(edit, editContext, fieldsContext) {
    editContext?.setSelectedComment(undefined);
    editContext?.setSelectedSuggestedEdit(edit);
    editContext?.setScrollIntoView(getSuggestedEditScrollIntoViewTarget(edit));
    if (edit.fieldId) {
        fieldsContext?.setFocusedField({
            fieldId: edit.fieldId,
            item: {
                id: edit.itemId,
                language: edit.mainItemLanguage,
                version: edit.mainItemVersion,
            },
        }, false);
        // Also select the item.
        editContext?.select?.([edit.itemId]);
    }
}
//# sourceMappingURL=feedbackSelection.js.map