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

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

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 React, { createContext, useMemo, useContext, useEffect, useCallback } from 'react';
import { inject } from 'mobx-react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { withRouter } from 'react-router';
import FormDataSet from "./FormDataSet";
import VersionsDataSet from "./VersionsDataSet";
import ValueDataSet from "./ValueDataSet";
var Store = /*#__PURE__*/createContext({});
export default Store;
export function useUpgradeStore() {
  return useContext(Store);
}
export var StoreProvider = withRouter(injectIntl(inject('AppState')(observer(function (props) {
  var projectId = props.AppState.currentMenuType.projectId,
      children = props.children,
      formatMessage = props.intl.formatMessage,
      defaultData = props.defaultData,
      _props$defaultData = props.defaultData,
      instanceId = _props$defaultData.instanceId,
      marketAppServiceId = _props$defaultData.marketAppServiceId,
      marketDeployObjectId = _props$defaultData.marketDeployObjectId,
      environmentId = _props$defaultData.environmentId,
      intlPrefix = props.intlPrefix,
      prefixCls = props.prefixCls,
      _props$isMiddleware = props.isMiddleware,
      isMiddleware = _props$isMiddleware === void 0 ? false : _props$isMiddleware;
  var versionsDs = useMemo(function () {
    return new _DataSet(VersionsDataSet({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix,
      projectId: projectId,
      marketDeployObjectId: marketDeployObjectId,
      marketAppServiceId: marketAppServiceId
    }));
  }, [projectId, marketDeployObjectId]);
  var valueDs = useMemo(function () {
    return new _DataSet(ValueDataSet({
      projectId: projectId,
      marketDeployObjectId: marketDeployObjectId,
      instanceId: instanceId
    }));
  }, [projectId, instanceId, marketDeployObjectId]);
  var formDs = useMemo(function () {
    return new _DataSet(FormDataSet({
      formatMessage: formatMessage,
      intlPrefix: intlPrefix,
      projectId: projectId,
      versionsDs: versionsDs,
      valueDs: valueDs,
      isMiddleware: isMiddleware
    }));
  }, [projectId]);
  useEffect(function () {
    var record = formDs.current;

    if (record) {
      record.init(_objectSpread(_objectSpread({}, defaultData), {}, {
        type: 'update'
      }));
    }

    loadVersion();
  }, []);
  var loadVersion = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var record;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            record = formDs.current;

            if (record) {
              _context.next = 3;
              break;
            }

            return _context.abrupt("return");

          case 3:
            _context.next = 5;
            return versionsDs.query();

          case 5:
            versionsDs.forEach(function (eachRecord) {
              var versionType = eachRecord.get('versionType');

              if (versionType === 'currentVersion') {
                record.init({
                  marketAppName: eachRecord.get('marketAppName'),
                  marketAppVersion: eachRecord.get('marketAppVersion')
                });
              }

              var versionTypeText = versionType ? "\uFF08".concat(formatMessage({
                id: "".concat(intlPrefix, ".market.version.").concat(versionType)
              }), "\uFF09") : '';
              eachRecord.init('marketServiceVersion', "".concat(eachRecord.get('marketServiceVersion')).concat(versionTypeText));
            });

          case 6:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })), [formDs.current]);

  var value = _objectSpread(_objectSpread({}, props), {}, {
    formatMessage: formatMessage,
    prefixCls: "".concat(prefixCls, "-instance-market"),
    projectId: projectId,
    formDs: formDs,
    versionsDs: versionsDs,
    defaultData: defaultData,
    valueDs: valueDs
  });

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