import type { ModuleId } from './moduleIds';
import type { AsukaRenderEngineCapabilityKey, AsukaRenderEngineKind } from '../core/renderEngine';
import type { AsukaModuleDefinition } from '../core/modules';

/** All supported overlay container types. */
export type OverlayType =
  | 'floating'
  | 'modal'
  | 'drawer'
  | 'panel-left'
  | 'panel-right'
  | 'panel-bottom'
  | 'panel-top'
  | 'popover'
  | 'inline';

/** Layout zones where draggable blocks can live. */
export type LayoutZoneId = 'top' | 'bottom' | 'left' | 'right';

/** One icon override entry accepted by {@link LayoutConfig.iconOverrides}. */
export interface LayoutIconOverride {
  /** Registered icon name. */
  name?: string;
  /** External icon URL. */
  url?: string;
  /** Inline SVG string. */
  svg?: string;
}

/** Executable module definition accepted only while the initial layout is being created. */
export type AsukaInlineLayoutModuleDefinition = Omit<AsukaModuleDefinition, 'id'>;

/** A draggable menu item declared inside a layout zone. */
export interface LayoutItemConfig {
  /** Unique item id used for drag, persistence and programmatic updates. */
  id?: string;
  /** Module id to open when clicking the item. */
  kind: ModuleId;
  /** Small label shown under the icon (Figma-like). */
  label?: string;
  /** Icon name, URL or serialized SVG token. */
  icon?: string;
  /** Visual order inside the parent zone. */
  order?: number;
  /** Responsive priority. Lower values stay visible first on compact mobile navigation. */
  priority?: number;
  /** Display container used when the module is opened. */
  type?: OverlayType;
  /** Optional menu-level engine filter. When omitted, the module definition controls compatibility. */
  supportedEngines?: AsukaRenderEngineKind[];
  /** Optional menu-level capability filter. When omitted, the module definition controls capability compatibility. */
  requiredCapabilities?: AsukaRenderEngineCapabilityKey[];
  /** Optional DOM selector used as the portal/mount target for this module overlay. */
  mountTarget?: string;
  /** Default collapsed state when the item is rendered inline or in a panel. */
  collapsed?: boolean;
  /** Render mode override for panel-like zones or persistent page portals. */
  renderMode?: 'inline' | 'portal';
  /**
   * Optional custom module registered automatically from this layout item.
   * The module id is derived from `kind`; this executable definition is never
   * copied into the serializable runtime layout state.
   */
  module?: AsukaInlineLayoutModuleDefinition;
}

/** Per-zone layout config used at instantiation. */
export interface LayoutZoneConfig {
  /** Default type used by the zone. */
  type?: OverlayType;
  /** List of draggable items to render in this zone. */
  items?: LayoutItemConfig[];
  /** Whether the zone is collapsible in the UI. */
  collapsible?: boolean;
  /** Default collapsed state. */
  collapsed?: boolean;
}

/** Selection menu settings used by the contextual object toolbar. */
export interface SelectionMenuConfig {
  /** Container type used to render the selection menu. */
  type?: OverlayType;
  /** Ordered list of modules shown in the selection menu. */
  items?: ModuleId[];
  /** Whether the selection menu should start opened. */
  open?: boolean;
}

/** Root layout config passed to createAsukaDesigner. */
export interface LayoutConfig {
  /** Optional icon overrides by module id or block id. */
  iconOverrides?: Record<string, string | LayoutIconOverride>;
  /** Top toolbar configuration. */
  top?: LayoutZoneConfig;
  /** Bottom toolbar configuration. */
  bottom?: LayoutZoneConfig;
  /** Left sidebar configuration. */
  left?: LayoutZoneConfig;
  /** Right sidebar configuration. */
  right?: LayoutZoneConfig;
  /** Contextual selection toolbar configuration. */
  selectionMenu?: SelectionMenuConfig;
}

/** Anchor box used by popovers and contextual overlays. */
export interface OverlayAnchorBox {
  left: number;
  top: number;
  width: number;
  height: number;
}

/** Arguments accepted by low-level overlay opening helpers. */
export type LayoutOpenArgs =
  | {
      /** Module id to open. */
      module: ModuleId;
      /** Optional display mode override. */
      type?: OverlayType;
      /** Optional DOM selector used as the portal/mount target for this overlay. */
      mountTarget?: string;
      /** Where to anchor a popover. */
      anchor?: 'selection' | OverlayAnchorBox | null;
      /** Optional title override used by the overlay header. */
      title?: string;
    }
  | {
      /** Named preset entry such as the contextual selection menu. */
      preset: 'selectionMenu';
      /** Optional display mode override. */
      type?: OverlayType;
      /** Optional DOM selector used as the portal/mount target for this overlay. */
      mountTarget?: string;
      /** Where to anchor a popover. */
      anchor?: 'selection' | OverlayAnchorBox | null;
      /** Optional title override used by the overlay header. */
      title?: string;
    };
