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; }

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; }

import { useLocalStore } from 'mobx-react-lite';
import { axios } from '@choerodon/boot';
import map from 'lodash/map';
export default function useStore(mainStore) {
  return useLocalStore(function () {
    return {
      mainStore: [],

      get getMainData() {
        this.setMainSource();
        return this.mainStore;
      },

      setMainSource: function setMainSource() {
        this.mainStore.stageList = this.dataSource;
      },
      setMainData: function setMainData(value) {
        this.mainStore = value;
      },
      loading: true,
      setLoading: function setLoading(value) {
        this.loading = value;
      },

      get getLoading() {
        return this.loading;
      },

      loadData: function loadData(projectId, pipelineId) {
        var _this = this;

        this.setLoading(true);
        this.loadDetail(projectId, pipelineId).then(function (res) {
          if (res) {
            var selectedId = mainStore.getSelectedMenu.id;

            if (selectedId === pipelineId) {
              var tempArr = res.devopsCiStageVOS.concat(res.devopsCdStageVOS);

              _this.setStepData(tempArr);

              _this.setViewData(tempArr);

              _this.setMainData(res);
            }

            _this.setLoading(false);
          }
        });
      },
      loadDetail: function loadDetail(projectId, pipelineId) {
        return axios.get("/devops/v1/projects/".concat(projectId, "/cicd_pipelines/").concat(pipelineId));
      },
      dataSource: [],
      viewData: [],
      setViewData: function setViewData(value) {
        this.viewData = value;
      },

      get getViewData() {
        var _this$viewData;

        return (_this$viewData = this.viewData) === null || _this$viewData === void 0 ? void 0 : _this$viewData.slice();
      },

      setStepData: function setStepData(value) {
        this.dataSource = value;
      },

      get getStepData() {
        var _this$dataSource;

        return (_this$dataSource = this.dataSource) === null || _this$dataSource === void 0 ? void 0 : _this$dataSource.slice();
      },

      addNewStep: function addNewStep(index, data) {
        var cdAuditUserIds = data.cdAuditUserIds;

        var stepObj = _objectSpread(_objectSpread({}, data), {}, {
          cdAuditUserIds: cdAuditUserIds && cdAuditUserIds.map(function (x) {
            return typeof x === 'object' ? x.id : x;
          }),
          name: data.step,
          jobList: []
        });

        this.dataSource.splice(index + 1, 0, stepObj);
        this.dataSource = this.dataSource.map(function (item, i) {
          var newItem = item;
          newItem.sequence = i;
          return newItem;
        });
      },
      removeStep: function removeStep(sequence) {
        var _this2 = this;

        this.dataSource.forEach(function (item, index) {
          if (item.sequence === sequence) {
            _this2.dataSource.splice(index, 1);

            return true;
          }

          return false;
        });
        this.dataSource = this.dataSource.map(function (item, i) {
          var newItem = item;
          newItem.sequence = i;
          return newItem;
        });
      },
      eidtStep: function eidtStep(sequence, newName, curType) {
        var _this3 = this;

        this.dataSource.forEach(function (item, index) {
          if (item.sequence === sequence) {
            _this3.dataSource[index].name = newName;
            _this3.dataSource[index].type = curType;
            return true;
          }

          return false;
        });
      },
      editStepLists: function editStepLists(lists) {
        this.dataSource = _toConsumableArray(lists);
      },
      newJob: function newJob(sequence, data) {
        var _this4 = this;

        this.dataSource.forEach(function (item, index) {
          if (item.sequence === sequence) {
            var _this4$dataSource$ind;

            // eslint-disable-next-line no-param-reassign
            data.sequence = _this4.dataSource[index].jobList.length;

            if ((_this4$dataSource$ind = _this4.dataSource[index]) === null || _this4$dataSource$ind === void 0 ? void 0 : _this4$dataSource$ind.jobList.length) {
              _this4.dataSource[index].jobList.push(data);
            } else {
              _this4.dataSource[index].jobList = [data];
            }
          }
        });
      },
      editJob: function editJob(sequence, key, data) {
        var _this5 = this;

        this.dataSource.forEach(function (item, index) {
          if (item.sequence === sequence) {
            _this5.dataSource[index].jobList[key] = _objectSpread({}, data);
          }
        });
      },
      editJobLists: function editJobLists(sequence, type, jobList) {
        if (type === 'CD') {
          var tempArr = map(jobList, function (item, index) {
            // eslint-disable-next-line no-param-reassign
            item.sequence = index;
            return item;
          });
          this.dataSource[sequence].jobList = _toConsumableArray(tempArr);
          return;
        }

        this.dataSource[sequence].jobList = _toConsumableArray(jobList);
      },
      removeStepTask: function removeStepTask(sequence, key) {
        var _this6 = this;

        this.dataSource.forEach(function (item, index) {
          if (item.sequence === sequence) {
            _this6.dataSource[index].jobList.splice(key, 1);
          }
        });
      }
    };
  });
}