import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
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/text-field/style";
import _TextField from "choerodon-ui/pro/lib/text-field";
import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";

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, { useState, Fragment } from 'react';
import { observer } from 'mobx-react-lite';
import { useRecordDetailStore } from "./stores";
import "./index.less";
export default observer(function () {
  var _useRecordDetailStore = useRecordDetailStore(),
      formatMessage = _useRecordDetailStore.intl.formatMessage,
      intlPrefix = _useRecordDetailStore.intlPrefix,
      prefixCls = _useRecordDetailStore.prefixCls,
      formDs = _useRecordDetailStore.formDs,
      modal = _useRecordDetailStore.modal,
      appServiceName = _useRecordDetailStore.appServiceName,
      appServiceId = _useRecordDetailStore.appServiceId;

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

  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            _context.next = 3;
            return formDs.submit();

          case 3:
            res = _context.sent;

            if (!(res !== false)) {
              _context.next = 6;
              break;
            }

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

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

          case 9:
            _context.prev = 9;
            _context.t0 = _context["catch"](0);
            return _context.abrupt("return", false);

          case 12:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[0, 9]]);
  })));
  modal.handleCancel(function () {
    if (formDs.dirty) {
      _Modal.open({
        key: _Modal.key(),
        title: formatMessage({
          id: 'prompt.inform.title'
        }),
        children: formatMessage({
          id: 'prompt.inform.message'
        }),
        onOk: function onOk() {
          return modal.close();
        }
      });

      return false;
    }
  });

  function handleAdd() {
    formDs.create();
  }

  function handleRemove(eachRecord) {
    formDs.remove(eachRecord);
  }

  function renderValue(_ref2) {
    var value = _ref2.value;

    if (value) {
      return showRevealValues ? value : '**********';
    }
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls)
  }, /*#__PURE__*/React.createElement(_Form, {
    style: {
      width: '70%'
    }
  }, appServiceId ? /*#__PURE__*/React.createElement(_TextField, {
    value: appServiceName,
    label: "\u5173\u8054\u5E94\u7528\u670D\u52A1",
    disabled: true
  }) : null), /*#__PURE__*/React.createElement(_Button, {
    funcType: "flat",
    color: "primary",
    className: "".concat(prefixCls, "-reveal"),
    onClick: function onClick() {
      return changeShowRevealValues(!showRevealValues);
    }
  }, formatMessage({
    id: "".concat(intlPrefix, ".settings.values.").concat(showRevealValues ? 'hide' : 'reveal')
  })), formDs.map(function (eachRecord) {
    return /*#__PURE__*/React.createElement(_Form, {
      record: eachRecord,
      columns: 14,
      key: eachRecord.id
    }, /*#__PURE__*/React.createElement(_TextField, {
      colSpan: 5,
      name: "key"
    }), /*#__PURE__*/React.createElement("span", {
      className: "".concat(prefixCls, "-equal")
    }, "="), /*#__PURE__*/React.createElement(_TextArea, {
      colSpan: 7,
      name: "value",
      renderer: renderValue,
      resize: "vertical",
      autoSize: {
        minRows: 1
      }
    }), formDs.length > 1 ? /*#__PURE__*/React.createElement(_Tooltip, {
      title: formatMessage({
        id: 'delete'
      })
    }, /*#__PURE__*/React.createElement(_Button, {
      funcType: "flat",
      icon: "delete",
      onClick: function onClick() {
        return handleRemove(eachRecord);
      },
      className: "c7ncd-form-record-delete-btn"
    })) : /*#__PURE__*/React.createElement("span", null));
  }), /*#__PURE__*/React.createElement(_Button, {
    funcType: "flat",
    color: "primary",
    icon: "add",
    onClick: handleAdd,
    className: "".concat(prefixCls, "-add-btn")
  }, formatMessage({
    id: "".concat(intlPrefix, ".settings.add")
  })));
});