import type {
  BasicFetchResult,
  BasicPageParams,
  BasicTreeOptionResult,
} from '#/api/model';

import { requestClient } from '#/api/request';

export namespace DictionaryApi {
  export interface ListParams {
    name?: string;
    code?: string;
  }
  export interface PageParams extends ListParams, BasicPageParams {}

  export interface BasicRecordItem {
    name: string;
    code: string;
    sort: number;
    remark: string;
  }

  export interface RecordItem extends BasicRecordItem {
    id: number;
  }

  export type PageResult = BasicFetchResult<RecordItem>;

  export const getTypePage = (params: PageParams) =>
    requestClient.get<PageResult>('/dictionary/type/page', { params });

  export const getTypeDetail = (id: number) =>
    requestClient.get<RecordItem>('/dictionary/type/entity', {
      params: { id },
    });

  export const addTypeDetail = (data: BasicRecordItem) =>
    requestClient.post('/dictionary/type', data);

  export const editTypeDetail = (data: RecordItem) =>
    requestClient.put('/dictionary/type', data);

  export const deleteTypeDetail = (id: number) =>
    requestClient.delete('/dictionary/type', { data: { id } });

  export const getItemTree = (params: ListParams) =>
    requestClient.get<RecordItem>('/dictionary/item/page', { params });

  export const getItemDetail = (id: number) =>
    requestClient.get<RecordItem>('/dictionary/item/entity', {
      params: { id },
    });

  export const addItemDetail = (data: BasicRecordItem) =>
    requestClient.post('/dictionary/item', data);

  export const editItemDetail = (data: RecordItem) =>
    requestClient.put('/dictionary/item', data);

  export const deleteItemDetail = (id: number) =>
    requestClient.delete('/dictionary/item', { data: { id } });

  export const getItemList = (code: string) =>
    requestClient.get<BasicTreeOptionResult[]>('/dictionary/item/list-code', {
      params: { code },
    });
}
