function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

import { useLocalStore } from 'mobx-react-lite';
import { axios } from '@choerodon/boot';
import findIndex from 'lodash/findIndex';
import find from 'lodash/find';
import filter from 'lodash/filter';
export default function useStore() {
  return useLocalStore(function () {
    return {
      navBounds: {},
      setNavBounds: function setNavBounds(data) {
        this.navBounds = data;
      },

      get getNavBounds() {
        return this.navBounds;
      },

      deleteArr: [],
      setDeleteArr: function setDeleteArr(data) {
        this.deleteArr = data;
      },

      get getDeleteArr() {
        return this.deleteArr;
      },

      deleteCheck: function deleteCheck(projectId, envId, objectType) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/notification/check_delete_resource?env_id=").concat(envId, "&object_type=").concat(objectType));
      },
      sendMessage: function sendMessage(projectId, envId, objectId, notificationId, objectType) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/notification/send_message?env_id=").concat(envId, "&object_id=").concat(objectId, "&notification_id=").concat(notificationId, "&object_type=").concat(objectType));
      },
      validateCaptcha: function validateCaptcha(projectId, envId, objectId, captcha, objectType) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/notification/validate_captcha?env_id=").concat(envId, "&object_id=").concat(objectId, "&captcha=").concat(captcha, "&object_type=").concat(objectType));
      },
      openDeleteModal: function openDeleteModal(envId, id, name, type, refresh) {
        var newDeleteArr = _toConsumableArray(this.deleteArr);

        var currentIndex = findIndex(newDeleteArr, function (item) {
          return id === item.deleteId && type === item.type;
        });

        if (currentIndex > -1) {
          var newItem = _objectSpread(_objectSpread({}, newDeleteArr[currentIndex]), {}, {
            display: true,
            refresh: refresh
          });

          newDeleteArr.splice(currentIndex, 1, newItem);
        } else {
          var _newItem = {
            display: true,
            deleteId: id,
            name: name,
            type: type,
            refresh: refresh,
            envId: envId
          };
          newDeleteArr.push(_newItem);
        }

        this.setDeleteArr(newDeleteArr);
      },
      closeDeleteModal: function closeDeleteModal(id, type) {
        var newDeleteArr = _toConsumableArray(this.deleteArr);

        var current = find(newDeleteArr, function (item) {
          return id === item.deleteId && type === item.type;
        });
        current.display = false;
        this.setDeleteArr(newDeleteArr);
      },
      removeDeleteModal: function removeDeleteModal(id, type) {
        var newDeleteArr = filter(this.deleteArr, function (_ref) {
          var deleteId = _ref.deleteId,
              objectType = _ref.type;
          return deleteId !== id || objectType !== type;
        });
        this.setDeleteArr(newDeleteArr);
      },
      deleteData: function deleteData(projectId, id, type, envId) {
        var url = {
          instance: "/devops/v1/projects/".concat(projectId, "/app_service_instances/").concat(id, "/delete"),
          service: "/devops/v1/projects/".concat(projectId, "/service/").concat(id),
          ingress: "/devops/v1/projects/".concat(projectId, "/ingress/").concat(id),
          certificate: "/devops/v1/projects/".concat(projectId, "/certifications?cert_id=").concat(id),
          configMap: "/devops/v1/projects/".concat(projectId, "/config_maps/").concat(id),
          secret: "/devops/v1/projects/".concat(projectId, "/secret/").concat(envId, "/").concat(id)
        };
        return axios["delete"](url[type]);
      }
    };
  });
}