/* eslint-disable max-len */
import { forEach, omit, map, difference, without } from 'lodash';
import { axios } from '@choerodon/boot';
import { useLocalStore } from 'mobx-react-lite';
import { Base64 } from 'js-base64';
export default function useStore() {
  return useLocalStore(function () {
    return {
      calculateType: function calculateType(typeArr) {
        var num = 0;
        forEach(typeArr, function (key) {
          switch (key) {
            case 'master':
              num += 4;
              break;

            case 'etcd':
              num += 2;
              break;

            case 'worker':
              num += 1;
              break;

            default:
              break;
          }
        });
        return num;
      },
      handleClusterCreateNodesInnerData: function handleClusterCreateNodesInnerData(data) {
        var _this = this;

        return map(data, function (item) {
          var tempItem = omit(item, ['__id', '__status', 'hasError']);
          tempItem.role = _this.calculateType(tempItem === null || tempItem === void 0 ? void 0 : tempItem.role); // 如果是密匙类型的话需要进行base64加密

          if (tempItem.authType === 'publickey') {
            tempItem.password = Base64.encode(tempItem.password);
          }

          return tempItem;
        });
      },
      handleClusterCreateNodesOutterData: function handleClusterCreateNodesOutterData(obj, hasOuterNode) {
        var tempArr;

        if (hasOuterNode) {
          tempArr = ['hostPort', 'hostIp', 'username', 'password', 'authType', 'type', 'innerNodeName'];
        } else {
          tempArr = ['hostPort', 'hostIp', 'username', 'password', 'authType', 'type'];
        }

        var tempObj = omit(obj, ['__id', '__status', 'hasError', 'status']); // 如果是密匙类型的话需要进行base64加密

        if (tempObj.authType === 'publickey') {
          tempObj.password = Base64.encode(tempObj.password);
        }

        var allKeys = difference(Object.keys(tempObj), tempArr);

        if (allKeys.length === 0) {
          return tempObj;
        }

        return null;
      },
      handleClusterByHostsData: function handleClusterByHostsData(value) {
        var _source$devopsCluster, _source$devopsCluster2, _source$devopsCluster3;

        var source = omit(JSON.parse(value), ['__id', '__status']);
        var hasInner = source === null || source === void 0 ? void 0 : (_source$devopsCluster = source.devopsClusterInnerNodeVOList) === null || _source$devopsCluster === void 0 ? void 0 : _source$devopsCluster.length;
        var hasOutter = source === null || source === void 0 ? void 0 : (_source$devopsCluster2 = source.devopsClusterOutterNodeVO) === null || _source$devopsCluster2 === void 0 ? void 0 : _source$devopsCluster2.length;
        var hasOuterNode = source === null || source === void 0 ? void 0 : (_source$devopsCluster3 = source.devopsClusterOutterNodeVO[0]) === null || _source$devopsCluster3 === void 0 ? void 0 : _source$devopsCluster3.innerNodeName;
        source.devopsClusterInnerNodeVOList = hasInner ? this.handleClusterCreateNodesInnerData(source === null || source === void 0 ? void 0 : source.devopsClusterInnerNodeVOList) : [];
        source.devopsClusterOutterNodeVO = hasOutter ? this.handleClusterCreateNodesOutterData(source === null || source === void 0 ? void 0 : source.devopsClusterOutterNodeVO[0], hasOuterNode) : null;
        return JSON.stringify(source);
      },
      checkClusterName: function checkClusterName(_ref) {
        var projectId = _ref.projectId,
            clusterName = _ref.clusterName;
        return axios.get("/devops/v1/projects/".concat(projectId, "/clusters/check_name?name=").concat(clusterName));
      },
      checkClusterCode: function checkClusterCode(_ref2) {
        var projectId = _ref2.projectId,
            clusterCode = _ref2.clusterCode;
        return axios.get("/devops/v1/projects/".concat(projectId, "/clusters/check_code?code=").concat(clusterCode));
      },
      checkNodeConnect: function checkNodeConnect(projectId, data) {
        return axios.post("/devops/v1/projects/".concat(projectId, "/nodes/connection_test"), JSON.stringify(data));
      }
    };
  });
}