import "choerodon-ui/pro/lib/text-area/style";
import _TextArea from "choerodon-ui/pro/lib/text-area";
import "choerodon-ui/pro/lib/button/style";
import _Button from "choerodon-ui/pro/lib/button";
import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/select-box/style";
import _SelectBox from "choerodon-ui/pro/lib/select-box";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
import "choerodon-ui/pro/lib/icon/style";
import _Icon from "choerodon-ui/pro/lib/icon";
import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
import "choerodon-ui/pro/lib/spin/style";
import _Spin from "choerodon-ui/pro/lib/spin";
import "choerodon-ui/pro/lib/select/style";
import _Select from "choerodon-ui/pro/lib/select";

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, { Fragment, useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import { Choerodon } from '@choerodon/boot';
import map from 'lodash/map';
import { useDomainFormStore } from "./stores";
import Tips from "../../../../../components/new-tips";
import "./index.less";
var Option = _Select.Option;
export default observer(function () {
  var _useDomainFormStore = useDomainFormStore(),
      formDs = _useDomainFormStore.formDs,
      pathListDs = _useDomainFormStore.pathListDs,
      serviceDs = _useDomainFormStore.serviceDs,
      annotationDs = _useDomainFormStore.annotationDs,
      formatMessage = _useDomainFormStore.intl.formatMessage,
      prefixCls = _useDomainFormStore.prefixCls,
      modal = _useDomainFormStore.modal,
      refresh = _useDomainFormStore.refresh,
      saveNetworkIds = _useDomainFormStore.saveNetworkIds;

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

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

  var isModify = record.status !== 'add';

  function formValidate() {
    return new Promise(function (resolve) {
      pathListDs.forEach( /*#__PURE__*/function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(pathRecord) {
          var res;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return pathRecord.getField('serviceId').checkValidity();

                case 2:
                  res = _context.sent;

                  if (!res) {
                    resolve(false);
                  } else {
                    resolve(true);
                  }

                case 4:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }));

        return function (_x) {
          return _ref.apply(this, arguments);
        };
      }());
    });
  }

  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
    var serviceIds;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            serviceIds = map(pathListDs.toData(), 'serviceId');
            saveNetworkIds && saveNetworkIds(serviceIds);
            _context2.prev = 2;
            _context2.t0 = !isModify;

            if (_context2.t0) {
              _context2.next = 9;
              break;
            }

            _context2.next = 7;
            return formValidate();

          case 7:
            _context2.t1 = _context2.sent;
            _context2.t0 = _context2.t1 === true;

          case 9:
            if (!_context2.t0) {
              _context2.next = 17;
              break;
            }

            _context2.next = 12;
            return formDs.submit();

          case 12:
            _context2.t2 = _context2.sent;

            if (!(_context2.t2 !== false)) {
              _context2.next = 16;
              break;
            }

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

          case 16:
            return _context2.abrupt("return", false);

          case 17:
            return _context2.abrupt("return", false);

          case 20:
            _context2.prev = 20;
            _context2.t3 = _context2["catch"](2);
            Choerodon.handleResponseError(_context2.t3);
            return _context2.abrupt("return", false);

          case 24:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2, null, [[2, 20]]);
  })));

  function handleAddPath() {
    pathListDs.create();
  }

  function handleAddAnnotation() {
    annotationDs.create();
  }

  function handleRemovePath(removeRecord) {
    pathListDs.remove(removeRecord);
  }

  function handleRemoveAnnotation(annotationRecord) {
    annotationDs.remove(annotationRecord);
  }

  function renderService(_ref3) {
    var serviceRecord = _ref3.serviceRecord,
        text = _ref3.text;
    var status = serviceRecord.get('status');
    var serviceError = serviceRecord.get('serviceError');
    return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Tooltip, {
      title: text
    }, /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-domain-form-network-text")
    }, text)), status && status !== 'running' && /*#__PURE__*/React.createElement(_Tooltip, {
      title: serviceError ? "failed: ".concat(serviceError) : formatMessage({
        id: status
      })
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: "error",
      className: "".concat(prefixCls, "-domain-form-network-status-icon")
    })));
  }

  function serviceOptionRender(_ref4) {
    var serviceRecord = _ref4.record,
        text = _ref4.text;
    var content = renderService({
      serviceRecord: serviceRecord,
      text: text
    });
    return content;
  }

  function serviceRender(_ref5) {
    var text = _ref5.text,
        value = _ref5.value;
    var serviceRecord = serviceDs.find(function (eachRecord) {
      return eachRecord.get('id') === value;
    });

    if (!serviceRecord) {
      return text;
    }

    var content = renderService({
      serviceRecord: serviceRecord,
      text: text
    });
    return content;
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-domain-form")
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "name",
    autoFocus: !isModify,
    disabled: isModify
  }), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-domain-form-text")
  }, /*#__PURE__*/React.createElement(Tips, {
    title: formatMessage({
      id: 'domain.protocol'
    }),
    helpText: formatMessage({
      id: 'domain.protocol.tip'
    })
  })), /*#__PURE__*/React.createElement(_SelectBox, {
    name: "isNormal",
    className: "".concat(prefixCls, "-domain-form-radio")
  }, /*#__PURE__*/React.createElement(Option, {
    value: true
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-domain-form-radio-text")
  }, formatMessage({
    id: 'domain.protocol.normal'
  }))), /*#__PURE__*/React.createElement(Option, {
    value: false
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-domain-form-radio-text")
  }, formatMessage({
    id: 'domain.protocol.secret'
  })))), /*#__PURE__*/React.createElement(_TextField, {
    name: "domain",
    autoFocus: isModify
  }), !record.get('isNormal') && /*#__PURE__*/React.createElement(_Select, {
    name: "certId",
    searchable: true
  })), map(pathListDs.data, function (pathRecord) {
    return /*#__PURE__*/React.createElement(_Form, {
      record: pathRecord,
      columns: 6,
      style: {
        width: '115%'
      },
      key: pathRecord.id
    }, /*#__PURE__*/React.createElement(_TextField, {
      name: "path",
      colSpan: 2,
      disabled: !record.get('domain')
    }), /*#__PURE__*/React.createElement(_Select, {
      name: "serviceId",
      searchable: true,
      colSpan: 2,
      optionRenderer: serviceOptionRender,
      renderer: serviceRender
    }), /*#__PURE__*/React.createElement(_Select, {
      name: "servicePort"
    }, map(pathRecord.get('ports'), function (_ref6) {
      var port = _ref6.port;
      return /*#__PURE__*/React.createElement(Option, {
        value: port,
        key: port
      }, port);
    })), pathListDs.length > 1 ? /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "delete",
      className: "c7ncd-form-record-delete-btn",
      onClick: function onClick() {
        return handleRemovePath(pathRecord);
      }
    }) : /*#__PURE__*/React.createElement("span", null));
  }), /*#__PURE__*/React.createElement(_Button, {
    funcType: "flat",
    color: "primary",
    icon: "add",
    onClick: handleAddPath
  }, formatMessage({
    id: 'domain.path.add'
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-domain-form-annotation-title")
  }, "Annotations"), map(annotationDs.data, function (annotationRecord) {
    return /*#__PURE__*/React.createElement(_Form, {
      columns: 14,
      record: annotationRecord,
      style: {
        width: '103.3%'
      },
      key: annotationRecord.id,
      className: "".concat(prefixCls, "-domain-form-annotation")
    }, /*#__PURE__*/React.createElement(_TextField, {
      colSpan: 6,
      name: "key"
    }), /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-domain-form-annotation-equal")
    }, "="), /*#__PURE__*/React.createElement(_TextArea, {
      colSpan: 6,
      name: "value",
      autoSize: {
        minRows: 1
      },
      resize: "vertical"
    }), annotationDs.length > 1 ? /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "delete",
      onClick: function onClick() {
        return handleRemoveAnnotation(annotationRecord);
      },
      className: "c7ncd-form-record-delete-btn"
    }) : /*#__PURE__*/React.createElement("span", null));
  }), /*#__PURE__*/React.createElement(_Button, {
    funcType: "flat",
    color: "primary",
    icon: "add",
    onClick: handleAddAnnotation
  }, formatMessage({
    id: 'domain.annotation.add'
  })));
});