import type { CreateFlowState } from "../../app/(app)/create/types";
import type { CustomMethodCardFieldBlock } from "./customMethodCardFieldBlocks";
import { isCatalogMethodCardId } from "./customRuleFacets";
import { isCustomMethodCardId } from "./isCustomMethodCardId";

/**
 * Create modals use {@link CustomMethodCardModalBody} when there are structured field
 * blocks for this method id (wizard-finalized cards, final-review chip edits, etc.),
 * including **proportion-only** layouts.
 *
 * Check persisted blocks **before** {@link isCustomMethodCardId}: `meta` can be absent
 * while `customMethodCardFieldBlocksById[id]` is still populated (e.g. partial merges).
 *
 * Duplicating a preset registers `meta` for the clone but leaves blocks empty — those
 * stubs keep the facet's structured edit fields until the user adds blocks (then this
 * returns true once persisted blocks are non-empty).
 *
 * Non-empty **draft** blocks also win, including catalog ids after Customize
 * Finalize (facet editors ignore array order).
 *
 * Catalog slugs (`video-meetings`, `signal`, …) may also have a meta row after
 * Customize writes a title override. Those cards keep the facet section editors
 * unless field blocks exist — matching catalog copy must not hide Core Principle /
 * Logistics / etc.
 *
 * **View mode** (`modalEditUnlocked` false): when a **user-authored** card still has
 * facet copy that matches empty preset seeds only (see
 * `./methodCardFacetMatchesPresetForId`), route to {@link CustomMethodCardModalBody}
 * so meta-only wizard cards show policy copy instead of empty section editors.
 * Pass `customFacetDetailsMatchPreset: false` when the caller knows facet details
 * were edited or cloned from a filled preset.
 *
 * Create-flow facet screens and final-review pass `modalEditUnlocked: true` so
 * section fields stay editable on open (Customize is the kebab wizard).
 */
export function usesWizardFieldBlocksModalBody(args: {
  methodId: string;
  meta: CreateFlowState["customMethodCardMetaById"];
  fieldBlocksById: CreateFlowState["customMethodCardFieldBlocksById"];
  modalEditUnlocked: boolean;
  draftFieldBlocks: readonly CustomMethodCardFieldBlock[] | null;
  /** When strictly `true` and modal is read-only, use wizard body for custom cards with empty blocks. */
  customFacetDetailsMatchPreset?: boolean;
}): boolean {
  const persisted = args.fieldBlocksById?.[args.methodId];
  if (Array.isArray(persisted) && persisted.length > 0) {
    return true;
  }
  if (
    args.draftFieldBlocks !== null &&
    args.draftFieldBlocks.length > 0
  ) {
    return true;
  }
  if (!isCustomMethodCardId(args.methodId, args.meta)) {
    return false;
  }
  if (isCatalogMethodCardId(args.methodId)) {
    return false;
  }
  return (
    !args.modalEditUnlocked && args.customFacetDetailsMatchPreset === true
  );
}
