import { defHttp } from '/@/utils/http/axios';
import { forEach, isNil, map } from 'lodash-es';
import { isObject } from '/@/utils/is';
import { limitationModel } from './algorithm.data';
// import { getEnfMineTree } from '/@/api/sys/menu';

enum Api {
  getMineData = '/province/mineData/getMineData',
  getCoalSeam = '/province/mineData/getCoalSeam',
  getCoalSeamAlarmRule = '/province/alarm/getCoalSeamAlarmRule',
  updateCoalSeamAlarmRule = '/province/alarm/updateCoalSeamAlarmRule',
  addCoalSeamAlarmRule = '/province/alarm/addCoalSeamAlarmRule',
  deleteCoalSeamAlarmRule = '/province/alarm/deleteCoalSeamAlarmRule',
  getGoafList = '/province/device/getGoafList',
  getGoafDataLimit = '/province/alarm/getGoafDataLimit',
  addGoafDataLimit = '/province/alarm/addGoafDataLimit',
  updateGoafDataLimit = '/province/alarm/updateGoafDataLimit',
  deleteGoafDataLimit = '/province/alarm/deleteGoafDataLimit',
  updateGoaf = '/province/device/updateGoaf',
}

export function getMineData(params: any) {
  return defHttp.post({ url: Api.getMineData, params });
}
export function getCoalSeam(params: any) {
  return defHttp.post({ url: Api.getCoalSeam, params }, { joinParamsToUrl: true });
}
export function getCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.getCoalSeamAlarmRule, params });
}
export function updateCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.updateCoalSeamAlarmRule, params });
}
export function addCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.addCoalSeamAlarmRule, params });
}
export function deleteCoalSeamAlarmRule(params: any) {
  return defHttp.post({ url: Api.deleteCoalSeamAlarmRule, params }, { joinParamsToUrl: true });
}
export function getGoafList(params: any) {
  return defHttp.post({ url: Api.getGoafList, params }, { joinParamsToUrl: true });
}
export function getGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.getGoafDataLimit, params }, { joinParamsToUrl: true });
}
export function addGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.addGoafDataLimit, params });
}
export function updateGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.updateGoafDataLimit, params });
}
export function updateGoaf(params: any) {
  return defHttp.post({ url: Api.updateGoaf, params });
}
export function deleteGoafDataLimit(params: any) {
  return defHttp.post({ url: Api.deleteGoafDataLimit, params }, { joinParamsToUrl: true });
}

function processLimitParams(params: Record<string, any>, add, edit) {
  const objectParams: any = {};

  forEach(params, (item, key) => {
    if (isObject(item)) {
      objectParams[key] = item;
    }
  });

  return Promise.all(
    map(objectParams, (item) => {
      if (item.id) {
        return edit(item);
      }
      if (!isNil(item.lowerLimit) || !isNil(item.upperLimit)) {
        return add(item);
      }
      return Promise.resolve();
    })
  );
}

export function patchGoafLimit(params: Record<string, any>) {
  return processLimitParams(params, addGoafDataLimit, updateGoafDataLimit);
}

export function patchCoalLimit(params: Record<string, any>) {
  return processLimitParams(params, addGoafDataLimit, updateGoafDataLimit);
}

function processLimitation(extra: Record<string, any>, data: any[]) {
  const result: any = {};
  forEach(limitationModel, (item, key) => {
    result[key] = { ...extra, ...item };
  });
  forEach(data, (item) => {
    result[item.alarmField] = item;
  });

  return result;
}

export function fetchGoafLimit(goafId: string, mineCode?: string) {
  return getGoafDataLimit({ goafId, mineCode }).then((r) => {
    return processLimitation({ goafId, mineCode }, r);
  });
}

export function fetchCoalLimit(goafId: string, mineCode?: string) {
  return getGoafDataLimit({ goafId, mineCode }).then((r) => {
    return processLimitation({ goafId, mineCode }, r);
  });
}

export function patchCoalSeamAlarmRule(params) {
  return params.id ? updateCoalSeamAlarmRule(params) : addCoalSeamAlarmRule(params);
}
