import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
import "choerodon-ui/pro/lib/select/style";
import _Select from "choerodon-ui/pro/lib/select";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
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, { useEffect, useMemo, useState, useCallback } from 'react';
import { observer } from 'mobx-react-lite';
import { Choerodon } from '@choerodon/boot';
import { useUpgradeStore } from "./stores";
import YamlEditor from "../../../../../../../components/yamlEditor";
var marketUpgrade = observer(function () {
  var _useUpgradeStore = useUpgradeStore(),
      formatMessage = _useUpgradeStore.formatMessage,
      projectId = _useUpgradeStore.projectId,
      intlPrefix = _useUpgradeStore.intlPrefix,
      prefixCls = _useUpgradeStore.prefixCls,
      formDs = _useUpgradeStore.formDs,
      refresh = _useUpgradeStore.refresh,
      modal = _useUpgradeStore.modal,
      valueDs = _useUpgradeStore.valueDs,
      _useUpgradeStore$defa = _useUpgradeStore.defaultData,
      marketAppServiceId = _useUpgradeStore$defa.marketAppServiceId,
      marketDeployObjectId = _useUpgradeStore$defa.marketDeployObjectId,
      search = _useUpgradeStore.location.search,
      versionsDs = _useUpgradeStore.versionsDs;

  var record = useMemo(function () {
    return formDs.current;
  }, [formDs.current]);

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      hasEditorError = _useState2[0],
      setHasEditorError = _useState2[1];

  useEffect(function () {
    modal.update({
      okProps: {
        disabled: !record || record.get('marketDeployObjectId') === marketDeployObjectId
      }
    });
  }, [record, record === null || record === void 0 ? void 0 : record.get('marketDeployObjectId')]);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!hasEditorError) {
              _context.next = 2;
              break;
            }

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

          case 2:
            _context.prev = 2;
            _context.next = 5;
            return formDs.submit();

          case 5:
            _context.t0 = _context.sent;

            if (!(_context.t0 !== false)) {
              _context.next = 9;
              break;
            }

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

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

          case 12:
            _context.prev = 12;
            _context.t1 = _context["catch"](2);
            Choerodon.handleResponseError(_context.t1);
            return _context.abrupt("return", false);

          case 16:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[2, 12]]);
  })));
  var getValue = useMemo(function () {
    var yaml = valueDs.current ? valueDs.current.get('yaml') : '';
    var values = record ? record.get('values') : '';
    return /*#__PURE__*/React.createElement(YamlEditor, {
      readOnly: false,
      value: values || yaml || '',
      originValue: yaml,
      handleEnableNext: handleNextStepEnable,
      onValueChange: handleChangeValue
    });
  }, [record, valueDs.current]);
  var handleNextStepEnable = useCallback(function (flag) {
    setHasEditorError(flag);
    modal.update({
      okProps: {
        disabled: flag
      }
    });
  }, []);
  var handleChangeValue = useCallback(function (value) {
    record && record.set('values', value);
  }, [record]);
  var getButtonContent = useMemo(function () {
    var spanDom = /*#__PURE__*/React.createElement("span", null, "\u67E5\u770B\u7248\u672C\u8BE6\u60C5");
    var deployObjectRecord = record ? versionsDs.find(function (eachRecord) {
      return eachRecord.get('id') === record.get('marketDeployObjectId');
    }) : null;
    var marketAppId = deployObjectRecord ? deployObjectRecord.get('marketAppId') : null;

    if (marketAppId) {
      return /*#__PURE__*/React.createElement("a", {
        href: "".concat(window.location.origin, "/#/market/app-market/app-detail/").concat(marketAppId).concat(search),
        rel: "nofollow me noopener noreferrer",
        target: "_blank",
        className: "".concat(prefixCls, "-btn-link")
      }, spanDom);
    }

    return spanDom;
  }, [record, search, versionsDs.records]);
  var renderVersion = useCallback(function (_ref2) {
    var value = _ref2.value,
        text = _ref2.text;

    if (value === text) {
      return '';
    }

    return text;
  }, []);

  if (!record) {
    return /*#__PURE__*/React.createElement(_Spin, {
      spinning: true
    });
  }

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs,
    columns: 3
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "marketAppName",
    disabled: true
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "marketAppVersion",
    disabled: true
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "marketServiceName",
    disabled: true,
    newLine: true
  }), /*#__PURE__*/React.createElement(_Select, {
    name: "marketDeployObjectId",
    clearButton: false,
    className: "".concat(prefixCls, "-select"),
    renderer: renderVersion
  }), /*#__PURE__*/React.createElement(_Button, {
    className: "".concat(prefixCls, "-btn"),
    disabled: !marketAppServiceId
  }, getButtonContent)), /*#__PURE__*/React.createElement(_Spin, {
    spinning: valueDs.status === 'loading'
  }, getValue));
});
export default marketUpgrade;