import { atom, useAtom } from 'jotai';

import { PaginationData, TableColumn, TableRowType } from '../shared/interface';

import { ModalType } from '../shared/types';
export const visibleColumnsAtom = atom<string[]>([]);
const toggleColumnMenuAtom = atom<boolean>(false);
export const useToggleColumn = () => {
  return useAtom(toggleColumnMenuAtom);
};

export const initialVisibleColumns = (tableData: TableColumn[]) => {
  return tableData.map(column => column.key);
};

// Dropdown
const PrintOptionTOggle = atom<number | null>(null);
export const usePrintOptionTOggle = () => {
  return useAtom(PrintOptionTOggle);
};

const menuDropDown = atom<boolean>(false);
export const useMenuDropDown = () => {
  return useAtom(menuDropDown);
};

//route page
const modalTypeAtom = atom<ModalType>('tablegrid');

export function useModalType() {
  return useAtom(modalTypeAtom);
}
// Overview Table
const atomRoutesTable = atom<TableRowType[]>([]);
export const useRoutesTable = () => {
  return useAtom(atomRoutesTable);
};
//this
const loading = atom<boolean>(false);
export const useStatsLoading = () => {
  return useAtom(loading);
};

//order return page
const loadingState = atom<boolean>(false);
export const useLoading = () => {
  return useAtom(loadingState);
};

const selectedAction = atom<string | null>(null);
export function useSelectedAction() {
  return useAtom(selectedAction);
}

const selectedLocation = atom<any[] | null>(null);
export function useSelectedLocation() {
  return useAtom(selectedLocation);
}

const selectedClient = atom<number | null>(null);
export function useSelectedClient() {
  return useAtom(selectedClient);
}

const clientTableId = atom<number | null>(null);
export function useIdForTableName() {
  return useAtom(clientTableId);
}

const clientTableName = atom<string | null>(null);
export function useTableName() {
  return useAtom(clientTableName);
}

// Paginations

const atomPaginationData = atom<PaginationData>({
  firstPage: 1,
  lastPage: 1,
  next_page: 1,
  page: 0,
  previous_page: 1,
  total: 0,
  totalPages: 0,
});
//client filter
export const UsePaginationData = () => {
  return useAtom(atomPaginationData);
};

const atomItemsPerPage = atom<number>(50);
export const useItemsPerPage = () => {
  return useAtom(atomItemsPerPage);
};

//return after order
export const UsePaginationReturnData = () => {
  return useAtom(atomPaginationData);
};

const atomItemsPerReturnPage = atom<number>(50);
export const useItemsPerReturnPage = () => {
  return useAtom(atomItemsPerReturnPage);
};

//stats data overview
export const UsePaginationOverviewData = () => {
  return useAtom(atomPaginationData);
};

const atomItemsPerOverviewPage = atom<number>(50);
export const useItemsPerOverviewPage = () => {
  return useAtom(atomItemsPerOverviewPage);
};

const countStatus = atom<any>([]);
export const useCountData = () => {
  return useAtom(countStatus);
};
