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/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/pro/lib/select-box/style";
import _SelectBox from "choerodon-ui/pro/lib/select-box";

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, { useImperativeHandle } from 'react';
import { observer } from 'mobx-react-lite';
import map from 'lodash/map';
import { useManualDeployStore } from "./stores";
import Tips from "../../../../components/new-tips";
var Option = _SelectBox.Option;
export default observer(function (_ref) {
  var cRef = _ref.cRef,
      otherEnvId = _ref.envId;

  var _useManualDeployStore = useManualDeployStore(),
      formatMessage = _useManualDeployStore.intl.formatMessage,
      prefixCls = _useManualDeployStore.prefixCls,
      manualDeployDs = _useManualDeployStore.manualDeployDs,
      pathListDs = _useManualDeployStore.pathListDs,
      domainDs = _useManualDeployStore.domainDs,
      annotationDs = _useManualDeployStore.annotationDs;

  var record = domainDs.current;
  var envId = manualDeployDs.current.get('environmentId');
  useImperativeHandle(cRef, function () {
    return {
      getDevopsIngressVO: function () {
        var _getDevopsIngressVO = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var domainResult, annotationResult, pathListResult, annotations;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  domainResult = domainDs.validate();
                  annotationResult = annotationDs.validate();
                  pathListResult = pathListDs.validate();

                  if (!(domainResult && annotationResult && pathListResult)) {
                    _context.next = 7;
                    break;
                  }

                  annotations = {};
                  annotationDs.toData().forEach(function (i) {
                    annotations[i.key] = i.value;
                  });
                  return _context.abrupt("return", {
                    devopsIngressVO: _objectSpread(_objectSpread({}, domainDs.current.toData()), {}, {
                      annotations: annotations,
                      pathList: pathListDs.toData()
                    })
                  });

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

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

        function getDevopsIngressVO() {
          return _getDevopsIngressVO.apply(this, arguments);
        }

        return getDevopsIngressVO;
      }()
    };
  });

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

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

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

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

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-resource-domain")
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: domainDs
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "name",
    disabled: !envId && !otherEnvId
  }), /*#__PURE__*/React.createElement(_SelectBox, {
    name: "isNormal",
    className: "".concat(prefixCls, "-resource-mgt-12")
  }, /*#__PURE__*/React.createElement(Option, {
    value: true
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-resource-radio")
  }, formatMessage({
    id: 'domain.protocol.normal'
  }))), /*#__PURE__*/React.createElement(Option, {
    value: false
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-resource-radio")
  }, formatMessage({
    id: 'domain.protocol.secret'
  })))), /*#__PURE__*/React.createElement(_TextField, {
    name: "domain",
    disabled: !envId && !otherEnvId
  }), !record.get('isNormal') && /*#__PURE__*/React.createElement(_Select, {
    name: "certId",
    disabled: !envId,
    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(_TextField, {
      name: "serviceName",
      colSpan: 2,
      disabled: true
    }), /*#__PURE__*/React.createElement(_Select, {
      name: "servicePort",
      disabled: !pathRecord.get('serviceName')
    }, map(pathRecord.get('ports'), function (port) {
      return /*#__PURE__*/React.createElement(Option, {
        value: port,
        key: port
      }, port);
    })), pathListDs.length > 1 ? /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "delete",
      className: "".concat(prefixCls, "-domain-form-delete 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, "-resource-domain-annotation-title")
  }, "Annotations"), map(annotationDs.data, function (annotationRecord) {
    return /*#__PURE__*/React.createElement(_Form, {
      columns: 29,
      record: annotationRecord,
      style: {
        width: '103.3%'
      },
      key: annotationRecord.id,
      className: "".concat(prefixCls, "-resource-domain-annotation")
    }, /*#__PURE__*/React.createElement(_TextField, {
      name: "key",
      colSpan: 13
    }), /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-resource-domain-annotation-equal")
    }, "="), /*#__PURE__*/React.createElement(_TextArea, {
      name: "value",
      autoSize: {
        minRows: 1
      },
      resize: "vertical",
      colSpan: 13
    }), annotationDs.length > 1 ? /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "delete",
      onClick: function onClick() {
        return handleRemoveAnnotation(annotationRecord);
      },
      className: "c7ncd-form-record-delete-btn",
      colSpan: 2
    }) : /*#__PURE__*/React.createElement("span", {
      colSpan: 2
    }));
  }), /*#__PURE__*/React.createElement(_Button, {
    funcType: "flat",
    color: "primary",
    icon: "add",
    onClick: handleAddAnnotation
  }, formatMessage({
    id: 'domain.annotation.add'
  })));
});