import { ReactNode, RefObject } from 'react';
import { RecoilState } from 'recoil';
import { BaseEntity, DataServicesConstructorEntity } from '@pos/base/domain/entities';
import { IBaseDataServicesRepository, IBaseTransformerRepository } from '@pos/base/domain/repositories';

import { FormInstance } from 'antd';
import { AgGridReact } from 'ag-grid-react';
import { RefreshServerSideParams } from 'ag-grid-community';

export interface ModuleConfigEntity {
  webUrl: string;
  moduleKey: string;
  tabTitle: string;
  dataServicesTransformer?: IBaseTransformerRepository;
  dataServicesConfig: DataServicesConstructorEntity;
  moduleType: 'single_page' | 'multiple_page';
  moduleCategory: 'default' | 'transaction';
  singlePageFormType?: 'modal' | 'drawer';
  singlePageDetailType?: 'modal' | 'drawer';

  // state definition
  metaIndexDataAtomState: RecoilState<any>;
  selectedIndexDataAtomState: RecoilState<any>;
  filterIndexDataAtomState: RecoilState<any>;

  // access control state definition
  privilegeAtomState: RecoilState<any>;
}

export interface ModuleProviderProps {
  configuration: ModuleConfigEntity;
  children?: ReactNode;
}

export interface SinglePageFormConfigEntity {
  open: boolean;
  dataId?: string;
  data?: any;
  type: 'create' | 'edit' | 'duplicate' | string;
}

export interface SinglePageDetailConfigEntity {
  open: boolean;
  dataId?: string;
  data?: any;
}

export interface UseAppModuleHookEntity<E extends BaseEntity = BaseEntity> {
  webUrl?: string;
  moduleKey: string;
  moduleType: 'single_page' | 'multiple_page';
  moduleCategory: 'default' | 'transaction';

  dataServices: IBaseDataServicesRepository;
  dataServicesConfig: DataServicesConstructorEntity;
  transformer?: IBaseTransformerRepository<E>;

  privilege: any;

  metaData: any;
  setMetaData(payload: any): void;
  filterData: any;
  setFilterData(payload: any): void;
  selectedData: any;
  setSelectedData(payload: any): void;

  formControl: FormInstance;
  formControlDetail: FormInstance;
  formControlFilter: FormInstance;

  singlePageFormType?: 'modal' | 'drawer';
  singlePageFormConfig: SinglePageFormConfigEntity;
  setSinglePageFormConfig(value: SinglePageFormConfigEntity): void;

  singlePageDetailType?: 'modal' | 'drawer';
  singlePageDetailConfig: SinglePageDetailConfigEntity;
  setSinglePageDetailConfig(value: SinglePageDetailConfigEntity): void;

  createAnotherData: boolean;
  setCreateAnotherData(value: boolean): void;

  loadingGetDataDetail: boolean;
  setLoadingGetDataDetail(value: boolean): void;

  loadingGetDataForm: boolean;
  setLoadingGetDataForm(value: boolean): void;

  indexGridRef: RefObject<AgGridReact>;
  handleRefreshIndexTable(params?: RefreshServerSideParams): void;

  gridConfiguration?: any;
  setGridConfiguration?(payload: any): void;
}
