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(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { Button, Form, Modal } from 'antd';
import React, { useMemo, useState } from 'react';
import Icon from "../Icon";
import { mergeMetadataTexts } from "../metadata/locale";
import MetadataForm from "../MetadataForm";
import MetadataPopoverList from "../MetadataPopoverList";
import "./index.less";
var iconMap = {
  labels: 'v35_Label',
  taints: 'v35_Tatint',
  annotations: 'v35_Annotation'
};
var MetadataCount = function MetadataCount(_ref) {
  var type = _ref.type,
    datasource = _ref.datasource,
    _ref$needEdit = _ref.needEdit,
    needEdit = _ref$needEdit === void 0 ? false : _ref$needEdit,
    _ref$valueRequired = _ref.valueRequired,
    valueRequired = _ref$valueRequired === void 0 ? false : _ref$valueRequired,
    onCallback = _ref.onCallback,
    _ref$isHasAuth = _ref.isHasAuth,
    isHasAuth = _ref$isHasAuth === void 0 ? true : _ref$isHasAuth,
    placement = _ref.placement,
    textsProp = _ref.texts;
  var t = useMemo(function () {
    return mergeMetadataTexts(textsProp);
  }, [textsProp]);
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isModalVisible = _useState2[0],
    setIsModalVisible = _useState2[1];
  var _Form$useForm = Form.useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    inputForm = _Form$useForm2[0];
  var titleNameMap = {
    labels: t.labelEdit,
    taints: t.taintEdit,
    annotations: t.annotationEdit
  };
  var handleSave = function handleSave() {
    inputForm.validateFields().then(function () {
      var values = inputForm.getFieldsValue();
      setIsModalVisible(false);
      onCallback === null || onCallback === void 0 || onCallback(values[type]);
    });
  };
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "metadata-count-wrap ".concat(isHasAuth && needEdit ? 'mc-min-width' : '')
  }, /*#__PURE__*/React.createElement("span", {
    className: "count-icon"
  }, /*#__PURE__*/React.createElement(Icon, {
    name: iconMap[type] || 'v35_Label'
  })), /*#__PURE__*/React.createElement(MetadataPopoverList, {
    datasource: datasource,
    type: type,
    kind: "count",
    placement: placement,
    texts: textsProp
  }), isHasAuth && needEdit && /*#__PURE__*/React.createElement(Button, {
    type: "text",
    size: "small",
    icon: /*#__PURE__*/React.createElement(Icon, {
      name: "v35_Edit"
    }),
    onClick: function onClick() {
      return setIsModalVisible(true);
    }
  })), /*#__PURE__*/React.createElement(Modal, {
    title: titleNameMap[type],
    open: isModalVisible,
    width: 600,
    onOk: handleSave,
    onCancel: function onCancel() {
      return setIsModalVisible(false);
    },
    destroyOnClose: true
  }, /*#__PURE__*/React.createElement(Form, {
    labelCol: {
      span: 4
    },
    form: inputForm
  }, /*#__PURE__*/React.createElement(Form.Item, null, /*#__PURE__*/React.createElement(MetadataForm, {
    type: type,
    form: inputForm,
    datasource: datasource,
    needTilte: true,
    valueRequired: valueRequired,
    texts: textsProp
  })))));
};
export default MetadataCount;