import type { CustomMethodCardFieldBlock } from "./customMethodCardFieldBlocks";

export type MethodCardHeaderDraft = {
  title: string;
  description: string;
};

/** Snapshot of modal-local edits taken when the user enters Customize mode. */
export type MethodCardCustomizeSnapshot<TDraft> = {
  pendingDraft: TDraft;
  fieldBlocks: CustomMethodCardFieldBlock[] | null;
  headerDraft: MethodCardHeaderDraft;
};

export function captureMethodCardCustomizeSnapshot<TDraft>(
  pendingDraft: TDraft,
  fieldBlocks: CustomMethodCardFieldBlock[] | null,
  headerDraft: MethodCardHeaderDraft,
): MethodCardCustomizeSnapshot<TDraft> {
  return {
    pendingDraft: structuredClone(pendingDraft),
    fieldBlocks:
      fieldBlocks === null ? null : structuredClone(fieldBlocks),
    headerDraft: { ...headerDraft },
  };
}

/**
 * True when a method-card create/customize modal is open with edits that
 * are not yet persisted — the condition for a tab/window `beforeunload` guard.
 */
export function isMethodCardCustomizeUnloadBlocked<TDraft>(
  modalOpen: boolean,
  snapshot: MethodCardCustomizeSnapshot<TDraft> | null,
  pendingDraft: TDraft | null,
  draftFieldBlocks: CustomMethodCardFieldBlock[] | null,
  headerDraft: MethodCardHeaderDraft | null,
): boolean {
  if (!modalOpen || snapshot === null) {
    return false;
  }
  return isMethodCardCustomizeSessionDirty(
    snapshot,
    pendingDraft,
    draftFieldBlocks,
    headerDraft,
  );
}

export function isMethodCardCustomizeSessionDirty<TDraft>(
  snapshot: MethodCardCustomizeSnapshot<TDraft>,
  pendingDraft: TDraft | null,
  draftFieldBlocks: CustomMethodCardFieldBlock[] | null,
  headerDraft: MethodCardHeaderDraft | null,
): boolean {
  if (!pendingDraft) {
    return false;
  }
  if (
    JSON.stringify(pendingDraft) !== JSON.stringify(snapshot.pendingDraft)
  ) {
    return true;
  }
  if (headerDraft !== null) {
    if (
      headerDraft.title !== snapshot.headerDraft.title ||
      headerDraft.description !== snapshot.headerDraft.description
    ) {
      return true;
    }
  }
  const cur =
    draftFieldBlocks === null ? null : JSON.stringify(draftFieldBlocks);
  const snap =
    snapshot.fieldBlocks === null ? null : JSON.stringify(snapshot.fieldBlocks);
  return cur !== snap;
}

/** For Close / overlay / Escape — skip closing when user cancels the confirm. */
export async function confirmDiscardMethodCardCustomizeSession<TDraft>(
  modalEditUnlocked: boolean,
  snapshot: MethodCardCustomizeSnapshot<TDraft> | null,
  pendingDraft: TDraft | null,
  draftFieldBlocks: CustomMethodCardFieldBlock[] | null,
  headerDraft: MethodCardHeaderDraft | null,
  confirmFn: () => Promise<boolean>,
): Promise<boolean> {
  if (!modalEditUnlocked || snapshot === null) {
    return true;
  }
  if (
    !isMethodCardCustomizeSessionDirty(
      snapshot,
      pendingDraft,
      draftFieldBlocks,
      headerDraft,
    )
  ) {
    return true;
  }
  return confirmFn();
}
