import "choerodon-ui/pro/lib/data-set/style";
import _DataSet from "choerodon-ui/pro/lib/data-set";

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 asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React, { createContext, useContext, useMemo, useEffect } from 'react';
import { inject } from 'mobx-react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { axios } from '@choerodon/boot';
import { useEnvironmentStore } from "../../../../stores";
import TableDataSet from "./TableDataSet";
import openWarnModal from "../../../../../../utils/openWarnModal";
var Store = /*#__PURE__*/createContext();
export function useEnvGroupStore() {
  return useContext(Store);
}
export var StoreProvider = injectIntl(inject('AppState')(observer(function (props) {
  var formatMessage = props.intl.formatMessage,
      children = props.children,
      projectId = props.AppState.currentMenuType.id;

  var _useEnvironmentStore = useEnvironmentStore(),
      intlPrefix = _useEnvironmentStore.intlPrefix,
      _useEnvironmentStore$ = _useEnvironmentStore.envStore,
      _useEnvironmentStore$2 = _useEnvironmentStore$.getSelectedMenu,
      id = _useEnvironmentStore$2.id,
      key = _useEnvironmentStore$2.key,
      getUpTarget = _useEnvironmentStore$.getUpTarget,
      setUpTarget = _useEnvironmentStore$.setUpTarget,
      treeDs = _useEnvironmentStore.treeDs;

  var groupDs = useMemo(function () {
    return new _DataSet(TableDataSet({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix
    }));
  }, []);

  function freshTree() {
    treeDs.query();
  }

  function checkGroupExist() {
    return _checkGroupExist.apply(this, arguments);
  }

  function _checkGroupExist() {
    _checkGroupExist = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var res;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (id) {
                _context.next = 2;
                break;
              }

              return _context.abrupt("return", true);

            case 2:
              _context.prev = 2;
              _context.next = 5;
              return axios.get("/devops/v1/projects/".concat(projectId, "/env_groups/").concat(id, "/check"));

            case 5:
              res = _context.sent;

              if (!(typeof res === 'boolean')) {
                _context.next = 9;
                break;
              }

              if (!res) {
                openWarnModal(freshTree, formatMessage);
              }

              return _context.abrupt("return", res);

            case 9:
              return _context.abrupt("return", true);

            case 12:
              _context.prev = 12;
              _context.t0 = _context["catch"](2);
              return _context.abrupt("return", true);

            case 15:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[2, 12]]);
    }));
    return _checkGroupExist.apply(this, arguments);
  }

  useEffect(function () {
    checkGroupExist().then(function (query) {
      if (query) {
        var param = id ? "?group_id=".concat(id) : '';
        groupDs.transport.read.url = "/devops/v1/projects/".concat(projectId, "/envs/list_by_group").concat(param);
        groupDs.query();
      }
    });
  }, [id, projectId]);
  useEffect(function () {
    if (getUpTarget === key) {
      groupDs.query();
      setUpTarget(null);
    }
  }, [getUpTarget]);

  var value = _objectSpread(_objectSpread({}, props), {}, {
    groupDs: groupDs
  });

  return /*#__PURE__*/React.createElement(Store.Provider, {
    value: value
  }, children);
})));