import { AllFilters, FiltersMap, ExtractFilterValue } from '@wix/bex-core';
import { InfoIconProps } from '@wix/design-system';

export type ColumnSortOrder = 'Ascending' | 'Descending';

export const columnSortOrder = {
  Ascending: 'Ascending' as const,
  Descending: 'Descending' as const,
};

export interface ColumnViewConfig {
  id: string;
  name?: string;
  isSelected?: boolean;
}

export interface ColumnSortConfig {
  columnId: string;
  direction: ColumnSortOrder;
}

export interface BasicCategory {
  id: string;
  /**
   *
   * What the category is called in the dropdown.
   * @external
   */
  name: string;
  /**
   * [InfoIconProps](https://www.docs.wixdesignsystem.com/?path=/story/components-form--infoicon)
   * @external
   */
  info?: InfoIconProps;
}

export interface Category<F extends FiltersMap> extends BasicCategory {
  /**
   * Unique identifier.
   * @external
   */
  id: string;

  /**
   * Array of [view objects](./?path=%2Fstory%2Fcommon-types--view) that appear under the category in the dropdown.
   * @external
   */
  views: View<F>[];
}

export type ViewFiltersValues<F extends FiltersMap> = {
  [P in keyof F]: {
    value?: ExtractFilterValue<F[P]>;
  } | null;
};

export interface ViewBase<F extends FiltersMap> {
  /**
   * Filters to apply when the view is selected.
   * @external
   */
  filters?: Partial<ViewFiltersValues<AllFilters<F>>> | null;
  orderedColumns?: ColumnViewConfig[];
  /**
   * Array of the selected columns.
   * @external
   */
  selectedColumns?: ColumnViewConfig[];
  /**
   * Array of sort directions.
   * @external
   */
  sortDirections?: ColumnSortConfig[];
}

export interface View<F extends FiltersMap> extends ViewBase<F> {
  /**
   * Unique identifier. Do not use category IDs, `saved-views`, or `predefined-views`.
   * @external
   */
  id: string;

  /**
   * What the view is called in the dropdown.
   * @external
   */
  name: string;

  /**
   * Whether the view is automatically applied when the table initially loads.
   * @external
   */
  isDefaultView?: boolean;
  createdAt?: string;
  /**
   * Extra data to be passed to the view.
   * @external
   */
  extra?: string;
}

export type AllItemsViewProps<F extends FiltersMap> = Omit<
  View<F>,
  'id' | 'createdAt' | 'filters' | 'isDefaultView' | 'name'
> & {
  /** A label to show instead of default 'All Items' */
  name?: string;

  /** Hides 'All Items' view from the list, default: false. Will be ignored if no presets are passed. */
  hidden?: boolean;
};

export type ViewActionNameBI =
  | 'view-save-changes'
  | 'view-save-new'
  | 'view-rename'
  | 'view-delete'
  | 'view-set-as-default'
  | 'view-unset-as-default';

export type PrivateView<F extends FiltersMap> = View<F> & {
  categoryId?: string;
  categorySortInd: string;
  isPreset: boolean;
};

export interface ViewsDTO<F extends FiltersMap> {
  hiddenPresetIds?: string[];
  views?: View<F>[];
}

export const ALL_ITEMS_VIEW_ID = 'all-items-view';
export const SAVED_VIEWS_ID = 'saved-views';
export const PREDEFINED_VIEWS_ID = 'predefined-views';

export const ALL_ITEMS_SORT_IND = '0';
export const SAVED_VIEWS_SORT_IND = '1';
export const PREDEFINED_VIEWS_SORT_IND = '2';
