import "choerodon-ui/pro/lib/select-box/style";
import _SelectBox from "choerodon-ui/pro/lib/select-box";
import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/password/style";
import _Password from "choerodon-ui/pro/lib/password";
import "choerodon-ui/pro/lib/spin/style";
import _Spin from "choerodon-ui/pro/lib/spin";
import "choerodon-ui/pro/lib/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
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, { useMemo } from 'react';
import { Choerodon } from '@choerodon/boot';
import { filter, forEach, map } from 'lodash';
import { observer } from 'mobx-react-lite';
import { usePrometheusStore } from "./stores";
import Tips from "../../../../../../../components/new-tips";
import "./index.less";
var Option = _Select.Option;
export default observer(function (props) {
  var _usePrometheusStore = usePrometheusStore(),
      formDs = _usePrometheusStore.formDs,
      modal = _usePrometheusStore.modal,
      refresh = _usePrometheusStore.refresh,
      prefixCls = _usePrometheusStore.prefixCls,
      formatMessage = _usePrometheusStore.intl.formatMessage,
      intlPrefix = _usePrometheusStore.intlPrefix,
      pvSelect = _usePrometheusStore.pvSelect,
      pvSelectEdit = _usePrometheusStore.pvSelectEdit,
      store = _usePrometheusStore.store,
      projectId = _usePrometheusStore.AppState.currentMenuType.projectId,
      clusterId = _usePrometheusStore.clusterId,
      showPassword = _usePrometheusStore.showPassword;

  var isModify = useMemo(function () {
    if (formDs.current) {
      return formDs.current.status !== 'add';
    }

    return false;
  }, [formDs.current]);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var postData, res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            postData = formDs.current.toData();
            _context.prev = 1;

            if (!(isModify && !formDs.dirty)) {
              _context.next = 8;
              break;
            }

            _context.next = 5;
            return store.installPrometheus(projectId, clusterId, postData);

          case 5:
            res = _context.sent;
            _context.next = 11;
            break;

          case 8:
            _context.next = 10;
            return formDs.submit();

          case 10:
            res = _context.sent;

          case 11:
            if (!(res !== false)) {
              _context.next = 14;
              break;
            }

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

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

          case 17:
            _context.prev = 17;
            _context.t0 = _context["catch"](1);
            Choerodon.handleResponseError(_context.t0);
            return _context.abrupt("return", false);

          case 21:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[1, 17]]);
  })));

  function handlePvFilter(optionRecord, name) {
    var pvId = optionRecord.get('id');
    var record = formDs.current;
    var arr = filter(pvSelect, function (item) {
      return item !== name;
    });
    var result = true;
    forEach(arr, function (item) {
      if (pvId === record.get(item)) {
        result = false;
      }
    });
    return result;
  }

  function getSelectContent(item) {
    var record = formDs.current;
    var _pvSelectEdit$item = pvSelectEdit[item],
        name = _pvSelectEdit$item.name,
        status = _pvSelectEdit$item.status;

    if (isModify && record.getPristineValue(name) && record.getPristineValue(item) && record.getPristineValue(status) !== 'Available') {
      return /*#__PURE__*/React.createElement(_TextField, {
        name: name,
        key: item,
        disabled: true
      });
    }

    return /*#__PURE__*/React.createElement(_Select, {
      key: item,
      name: item,
      searchable: true,
      optionsFilter: function optionsFilter(optionRecord) {
        return handlePvFilter(optionRecord, item);
      }
    });
  }

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

  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs
  }, (!isModify || showPassword) && /*#__PURE__*/React.createElement(_Password, {
    name: "adminPassword",
    autoFocus: true
  }), /*#__PURE__*/React.createElement(_TextField, {
    name: "grafanaDomain",
    autoFocus: isModify
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-monitor-create-pv")
  }, /*#__PURE__*/React.createElement(Tips, {
    title: formatMessage({
      id: "".concat(intlPrefix, ".monitor.pv")
    }),
    helpText: formatMessage({
      id: "".concat(intlPrefix, ".monitor.pv.tips")
    })
  })), /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs
  }, map(pvSelect, function (item) {
    return getSelectContent(item);
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-monitor-create-https")
  }, /*#__PURE__*/React.createElement(Tips, {
    title: formatMessage({
      id: "".concat(intlPrefix, ".monitor.https")
    }),
    helpText: formatMessage({
      id: "".concat(intlPrefix, ".monitor.https.tips")
    })
  })), /*#__PURE__*/React.createElement(_Form, {
    dataSet: formDs
  }, /*#__PURE__*/React.createElement(_SelectBox, {
    name: "enableTls"
  }, /*#__PURE__*/React.createElement(Option, {
    value: true
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-monitor-create-https-span")
  }, formatMessage({
    id: "".concat(intlPrefix, ".monitor.https.true")
  }))), /*#__PURE__*/React.createElement(Option, {
    value: false
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefixCls, "-monitor-create-https-span")
  }, formatMessage({
    id: "".concat(intlPrefix, ".monitor.https.false")
  }))))));
});