import "choerodon-ui/lib/spin/style";
import _Spin from "@hzero-front-ui/c7n-ui/lib/Spin";

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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

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

import React, { useState, Fragment } from 'react';
import { observer } from 'mobx-react-lite';
import { Choerodon } from '@choerodon/boot';
import YamlEditor from "../../../../../../../components/yamlEditor";
import InterceptMask from "../../../../../../../components/intercept-mask";
import { handlePromptError } from "../../../../../../../utils";
import "./index.less";
var ValueModalContent = observer(function (_ref) {
  var modal = _ref.modal,
      store = _ref.store,
      formatMessage = _ref.formatMessage,
      intlPrefix = _ref.intlPrefix,
      prefixCls = _ref.prefixCls,
      vo = _ref.vo,
      refresh = _ref.refresh,
      isMarket = _ref.isMarket,
      isMiddleware = _ref.isMiddleware;

  var _useState = useState(''),
      _useState2 = _slicedToArray(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1];

  var _useState3 = useState(false),
      _useState4 = _slicedToArray(_useState3, 2),
      isDisabled = _useState4[0],
      setIsDisabled = _useState4[1];

  var _useState5 = useState(false),
      _useState6 = _slicedToArray(_useState5, 2),
      isLoading = _useState6[0],
      setIsLoading = _useState6[1];

  var _store$getUpgradeValu = store.getUpgradeValue,
      yaml = _store$getUpgradeValu.yaml,
      name = _store$getUpgradeValu.name;
  modal.handleOk(handleOk); // eslint-disable-next-line consistent-return

  function handleOk() {
    return _handleOk.apply(this, arguments);
  }

  function _handleOk() {
    _handleOk = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var id, parentId, projectId, appServiceVersionId, appServiceId, _parentId$split, _parentId$split2, envId, data, result;

      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!isDisabled) {
                _context.next = 2;
                break;
              }

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

            case 2:
              setIsLoading(true);
              id = vo.id, parentId = vo.parentId, projectId = vo.projectId, appServiceVersionId = vo.appServiceVersionId, appServiceId = vo.appServiceId;
              _parentId$split = parentId.split('**'), _parentId$split2 = _slicedToArray(_parentId$split, 1), envId = _parentId$split2[0];
              data = {
                values: value || yaml || '',
                instanceId: id,
                type: 'update',
                environmentId: envId
              };

              if (isMarket || isMiddleware) {
                data.marketAppServiceId = appServiceId;
                data.marketDeployObjectId = appServiceVersionId;
              } else {
                data.appServiceId = appServiceId;
                data.appServiceVersionId = appServiceVersionId;
              }

              _context.prev = 7;
              _context.next = 10;
              return store.upgrade(projectId, data, isMarket, isMiddleware);

            case 10:
              result = _context.sent;

              if (handlePromptError(result)) {
                Choerodon.prompt('修改成功.');
                refresh();
              } else {
                Choerodon.prompt('修改失败.');
              }

              _context.next = 17;
              break;

            case 14:
              _context.prev = 14;
              _context.t0 = _context["catch"](7);
              Choerodon.handleResponseError(_context.t0);

            case 17:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[7, 14]]);
    }));
    return _handleOk.apply(this, arguments);
  }

  function toggleOkDisabled(flag) {
    modal.update({
      okProps: {
        disabled: flag
      }
    });
  }

  function handleChange(nextValue) {
    setValue(nextValue);
  }

  function handleEnableNext(flag) {
    setIsDisabled(flag);
    toggleOkDisabled(flag);
  }

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-instance-upgrade-tips")
  }, /*#__PURE__*/React.createElement("strong", null, "\u6CE8\u610F\uFF1A"), /*#__PURE__*/React.createElement("span", null, /*#__PURE__*/React.createElement("br", null), "- \u5728\u53D8\u66F4\u5B9E\u4F8B\u65F6\uFF0CChart\u5305\u5185\u6216\u8005values\u4E2D\u63A7\u5236\u526F\u672C\u6570\u91CF\u7684\u914D\u7F6E\u5C06\u4E0D\u4F1A\u751F\u6548\uFF0C\u800C\u662F\u4F1A\u548C\u73B0\u6709\u751F\u6548\u7684\u5B9E\u4F8B\u7684\u526F\u672C\u6570\u4FDD\u6301\u4E00\u81F4\u3002", /*#__PURE__*/React.createElement("br", null), "- \u82E5\u60F3\u4FEE\u6539\u526F\u672C\u6570\u91CF\uFF0C\u8BF7\u5728\u90E8\u7F72\u540E\u524D\u5F80\u8FD0\u884C\u8BE6\u60C5\u9875\u9762\u4E2D\u66F4\u6539Pod\u6570\u91CF\u5373\u53EF\u3002", /*#__PURE__*/React.createElement("br", null), "- \u4E0B\u65B9values\u4E2D\u5176\u4ED6\u53C2\u6570\u5B57\u6BB5\u4FEE\u6539\u540E\u4F9D\u7136\u4F1A\u751F\u6548\u3002")), /*#__PURE__*/React.createElement(_Spin, {
    spinning: store.getValueLoading
  }, /*#__PURE__*/React.createElement(YamlEditor, {
    readOnly: false,
    value: value || yaml || '',
    originValue: yaml,
    onValueChange: handleChange,
    handleEnableNext: handleEnableNext
  })), /*#__PURE__*/React.createElement(InterceptMask, {
    visible: isLoading
  }));
});
export default ValueModalContent;