import { StoragePrivilege } from '@pos/base/data/local-data-services';
import { omit } from 'lodash';
export * from './access-control.constant';

export const DefaultAccessControl = {
  view_index: false,
  view_detail: false,
  create: false,
  update: false,
  delete: false,
  change_status: false,
  confirm: false,
  view_external_link: false,
  download: false,
};

export const DefaultAccessControlTrue = {
  view_index: true,
  view_detail: true,
  create: true,
  update: true,
  delete: true,
  change_status: true,
  confirm: true,
  view_external_link: true,
  download: true,
};

export enum ControlTypeEnum {
  view_index = 'view_index',
  view_detail = 'view_detail',
  create = 'create',
  update = 'update',
  delete = 'delete',
  change_status = 'change_status',
  confirm = 'confirm',
  view_external_link = 'view_external_link',
  download = 'download',
}

export type ControlType = {
  view_index: boolean;
  view_detail: boolean;
  create: boolean;
  update: boolean;
  delete: boolean;
  change_status: boolean;
  confirm: boolean;
  view_external_link: boolean;
  download: boolean;
};

interface GetProps {
  moduleKey: string;
}
export function getAccessControlByModule(props: GetProps): ControlType {
  const { moduleKey } = props;
  const list: any[] = StoragePrivilege.get() as any;
  const control = list?.find((el: any) => el.module_key === moduleKey) ?? DefaultAccessControl;
  const fixControl = omit(control, ['module_key']) as ControlType;
  return fixControl;
}
