export type AsukaViewsPaginationMode = 'auto' | 'thumbnails' | 'dots' | 'numbers';

export interface AsukaViewsPaginationOptions {
  /** Show the view pagination bar below the canvas. Defaults to true. */
  enabled?: boolean;
  /**
   * Pagination visual mode.
   * - `auto`: thumbnails when available, dots otherwise.
   * - `thumbnails`: always show view thumbnails.
   * - `dots`: show compact dots.
   * - `numbers`: show compact numbered pills.
   */
  mode?: AsukaViewsPaginationMode;
  /** Show view labels below thumbnails. Defaults to false. */
  showLabels?: boolean;
  /** Allow horizontal grab/drag gestures to switch views. Defaults to true. */
  enableGrabNavigation?: boolean;
}

export interface AsukaHistoryControlsOptions {
  /** Show the undo control. Defaults to true. */
  undo?: boolean;
  /** Show the redo control. Defaults to true. */
  redo?: boolean;
  /** Show the reset control. Defaults to true. */
  reset?: boolean;
}

export interface AsukaUiOptions {
  /** Configure or disable the view pagination bar below the canvas. */
  viewsPagination?: boolean | AsukaViewsPaginationOptions;
  /** Configure or hide the history controls over the canvas. */
  historyControls?: boolean | AsukaHistoryControlsOptions;
}

export interface NormalizedAsukaViewsPaginationOptions {
  enabled: boolean;
  mode: AsukaViewsPaginationMode;
  showLabels: boolean;
  enableGrabNavigation: boolean;
}

export interface NormalizedAsukaUiOptions {
  viewsPagination: NormalizedAsukaViewsPaginationOptions;
  historyControls: Required<AsukaHistoryControlsOptions>;
}

export const DEFAULT_ASUKA_UI_OPTIONS: NormalizedAsukaUiOptions = {
  viewsPagination: {
    enabled: true,
    mode: 'auto',
    showLabels: false,
    enableGrabNavigation: true,
  },
  historyControls: {
    undo: true,
    redo: true,
    reset: true,
  },
};

const isViewsPaginationMode = (value: unknown): value is AsukaViewsPaginationMode =>
  value === 'auto' || value === 'thumbnails' || value === 'dots' || value === 'numbers';

export function normalizeAsukaUiOptions(input?: AsukaUiOptions | null): NormalizedAsukaUiOptions {
  const raw = input && typeof input === 'object' ? input : {};
  const rawPagination = (raw as AsukaUiOptions).viewsPagination;
  const paginationObject = rawPagination && typeof rawPagination === 'object' ? rawPagination as AsukaViewsPaginationOptions : {};
  const rawHistoryControls = (raw as AsukaUiOptions).historyControls;
  const historyControlsObject = rawHistoryControls && typeof rawHistoryControls === 'object'
    ? rawHistoryControls as AsukaHistoryControlsOptions
    : {};

  const modeCandidate = paginationObject.mode;

  const enabled = rawPagination === false
    ? false
    : rawPagination === true
      ? true
      : paginationObject.enabled !== false;

  return {
    viewsPagination: {
      ...DEFAULT_ASUKA_UI_OPTIONS.viewsPagination,
      enabled,
      mode: isViewsPaginationMode(modeCandidate) ? modeCandidate : DEFAULT_ASUKA_UI_OPTIONS.viewsPagination.mode,
      showLabels: paginationObject.showLabels === true,
      enableGrabNavigation: paginationObject.enableGrabNavigation !== false,
    },
    historyControls: rawHistoryControls === false
      ? { undo: false, redo: false, reset: false }
      : {
          undo: historyControlsObject.undo !== false,
          redo: historyControlsObject.redo !== false,
          reset: historyControlsObject.reset !== false,
        },
  };
}
