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/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
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";
var Option = _SelectBox.Option;
export default observer(function (_ref) {
  var cRef = _ref.cRef,
      envId = _ref.envId;

  var _useManualDeployStore = useManualDeployStore(),
      formatMessage = _useManualDeployStore.intl.formatMessage,
      prefixCls = _useManualDeployStore.prefixCls,
      manualDeployDs = _useManualDeployStore.manualDeployDs,
      portsDs = _useManualDeployStore.portsDs,
      networkDs = _useManualDeployStore.networkDs;

  useImperativeHandle(cRef, function () {
    return {
      getDevopsServiceReqVO: function () {
        var _getDevopsServiceReqVO = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
          var newworkResult, portsResult;
          return regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  newworkResult = networkDs.validate();
                  portsResult = portsDs.validate();

                  if (!(newworkResult && portsResult)) {
                    _context.next = 4;
                    break;
                  }

                  return _context.abrupt("return", {
                    devopsServiceReqVO: _objectSpread(_objectSpread({}, networkDs.current.toData()), {}, {
                      externalIp: networkDs.current.get('externalIp').join(','),
                      ports: portsDs.toData()
                    })
                  });

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

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

        function getDevopsServiceReqVO() {
          return _getDevopsServiceReqVO.apply(this, arguments);
        }

        return getDevopsServiceReqVO;
      }()
    };
  });
  var record = networkDs.current;

  function handleRemovePort(portRecord) {
    portsDs.remove(portRecord);
    portsDs.validate();
  }

  function handlePortAdd() {
    portsDs.create();
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-resource-network")
  }, /*#__PURE__*/React.createElement(_Form, {
    dataSet: networkDs
  }, /*#__PURE__*/React.createElement(_TextField, {
    name: "name",
    disabled: !manualDeployDs.current.get('environmentId') && !envId
  }), /*#__PURE__*/React.createElement(_SelectBox, {
    name: "type",
    className: "".concat(prefixCls, "-resource-mgt-12")
  }, /*#__PURE__*/React.createElement(Option, {
    value: "ClusterIP"
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-resource-radio")
  }, "ClusterIP")), /*#__PURE__*/React.createElement(Option, {
    value: "NodePort"
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-resource-radio")
  }, "NodePort")), /*#__PURE__*/React.createElement(Option, {
    value: "LoadBalancer"
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-resource-radio")
  }, "LoadBalancer"))), record.get('type') === 'ClusterIP' && /*#__PURE__*/React.createElement(_TextField, {
    name: "externalIp"
  })), map(portsDs.data, function (portRecord) {
    return /*#__PURE__*/React.createElement(_Form, {
      record: portRecord,
      key: portRecord.id,
      columns: 5
    }, record.get('type') !== 'ClusterIP' && /*#__PURE__*/React.createElement(_TextField, {
      name: "nodePort"
    }), /*#__PURE__*/React.createElement(_TextField, {
      name: "port"
    }), /*#__PURE__*/React.createElement(_TextField, {
      name: "targetPort"
    }), record.get('type') === 'NodePort' && /*#__PURE__*/React.createElement(_Select, {
      name: "protocol"
    }), portsDs.length > 1 ? /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "delete",
      onClick: function onClick() {
        return handleRemovePort(portRecord);
      },
      className: "".concat(prefixCls, "-resource-delete-btn c7ncd-form-record-delete-btn"),
      colSpan: 3
    }) : /*#__PURE__*/React.createElement("span", {
      colSpan: 3
    }));
  }), /*#__PURE__*/React.createElement(_Button, {
    color: "primary",
    funcType: "flat",
    onClick: handlePortAdd,
    icon: "add"
  }, formatMessage({
    id: 'network.config.addport'
  })));
});