import React__default, { useState, useEffect } from 'react';
import ___default from 'lodash';
import { C as CheckList } from '../index-6ef7005c.js';
import { Row, Col } from 'antd';
import { Service as Service$1 } from '@zenview/micro-basic-library';
import { cache } from '@zenview/micro-utils';
import '../_rollupPluginBabelHelpers-736b301a.js';
import '../index-f41abfce.js';
import 'react-virtualized';
import '../CBox/index.js';
import '../TreeTitle/index.js';
import '../SearchInput/index.js';
import '../IconFont/index.js';
import '../HightLevel/index.js';

const getHeaders = () => {
  return {
    Authorization: cache.getCache('token', 'session')
  };
};
class AppFunListService {
  queryFunctionByCondition(data) {
    return Service$1.$http({
      data,
      headers: getHeaders(),
      url: `/micro/function/v1/queryFunctionByCondition`,
      method: 'post',
      requestId: 'adminManager'
    }).then(res => {
      return res;
    }).catch(e => {
      return Promise.reject(e);
    });
  }
  queryMicroApplicationList() {
    return Service$1.$http({
      headers: getHeaders(),
      url: `/micro/microApplication/v1/queryMicroApplicationList`,
      method: 'get',
      requestId: 'adminManager'
    }).then(res => {
      return res;
    }).catch(e => {
      return Promise.reject(e);
    });
  }
}
var Service = new AppFunListService();

/**
 * @desc 微应用-功能菜单-两列
 * @param {Object} option
 * @param { Array } option.checkedList 功能列表默认选中项
 * @param {Function} option.checkClick 功能列表点击事件
 * @param {Function} option.changeApp 切换app。计算选中列表
 */
function AppFunList({
  checkedList = [],
  checkClick,
  changeApp
}) {
  const [appAll, setAppAll] = useState([]); // 微应用列表
  const [funAll, setFunAll] = useState([]); // 当前、所有微应用下的功能模板
  const [keywords, setKeywords] = useState(''); // 左侧微应用筛选条件
  const [funKeywords, setFunKeywords] = useState(undefined); // 右侧功能列表筛选条件
  const [appActiveKey, setAppActiveKey] = useState('all'); // 左侧微应用列表选中项
  const [appList, setAppList] = useState([]); // 左侧微应用展示列表
  const [funcList, setFuncList] = useState([]); // 右侧功能项列表
  const [checkedKeys, setCheckedKeys] = useState([]); // 右侧功能选中项目
  let debounced = null; // 搜索延迟的效果

  useEffect(() => {
    init();
  }, []);
  useEffect(() => {
    // 去除掉当前列表中没有的CheckedKeys
    let list = checkedList.filter(v => funAll.find(m => m.code === v));
    setCheckedKeys(list);
  }, [checkedList, funAll]);
  const init = () => {
    Promise.all([Service.queryMicroApplicationList(), Service.queryFunctionByCondition({
      "isMenu": 1
    })]).then(res => {
      setAppAll(res[0].data);
      setAppList(res[0].data);
      setFunAll(res[1].data);
      setFuncList(res[1].data);
    });
  };
  const onChangeApp = value => {
    setKeywords(value);
    const searchKeyword = () => {
      setAppList(value ? appAll.filter(v => v.name.includes(value)) : appAll);
    };
    debouncedFun(searchKeyword);
  };
  const onChangeFun = value => {
    setFunKeywords(value);
    setAppList(JSON.parse(JSON.stringify(appList))); // 用来触发click事件，勿删，否则 VList.CheckList 组件触发onClickItem获取不到funKeywords
    const searchKeyword = () => {
      setFuncList(value ? funAll.filter(v => v.name.includes(value)) : funAll);
    };
    debouncedFun(searchKeyword);
  };
  const debouncedFun = searchKeyword => {
    debounced && debounced.cancel();
    debounced = ___default.debounce(searchKeyword, 150);
    debounced();
  };
  const onChecked = (keys, values) => {
    setCheckedKeys(keys);
    checkClick && checkClick(funcList.filter(v => keys.includes(v.code)), values);
  };
  const onClickApp = async data => {
    let option = {
      "isMenu": 1
    };
    if (appActiveKey !== (data === null || data === void 0 ? void 0 : data.id)) {
      setAppActiveKey(data === null || data === void 0 ? void 0 : data.id);
      option.microApplicationId = data === null || data === void 0 ? void 0 : data.id;
    } else {
      setAppActiveKey('all');
    }
    let res = await Service.queryFunctionByCondition(option);
    setFunAll(res.data);
    setFuncList(funKeywords ? res.data.filter(v => v.name.includes(funKeywords)) : res.data);
    changeApp && changeApp(res.data);
  };
  return /*#__PURE__*/React__default.createElement(Row, {
    className: "mic-com-app-fun-all"
  }, /*#__PURE__*/React__default.createElement(Col, {
    span: 12,
    style: boxStyle
  }, /*#__PURE__*/React__default.createElement(CheckList, {
    list: appList,
    onClickItem: onClickApp,
    activeKey: appActiveKey,
    title: `微应用列表`,
    placeholder: "\u8F93\u5165\u5FAE\u5E94\u7528\u5173\u952E\u5B57\u8FDB\u884C\u641C\u7D22",
    checkable: false,
    itemKey: "id",
    itemNameKey: "name",
    onChange: onChangeApp,
    keywords: keywords
  })), /*#__PURE__*/React__default.createElement(Col, {
    span: 12,
    style: boxStyle
  }, /*#__PURE__*/React__default.createElement(CheckList, {
    list: funcList,
    checkedKeys: checkedKeys,
    onChecked: onChecked,
    keywords: funKeywords,
    title: `已选中第一个`,
    placeholder: "\u8F93\u5165\u529F\u80FD\u540D\u8FDB\u884C\u641C\u7D22",
    checkable: true,
    itemKey: "code",
    itemNameKey: "name",
    onChange: onChangeFun
  })));
}
const boxStyle = {
  height: "100%"
};

export { AppFunList as default };
//# sourceMappingURL=index.js.map
