import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";
import "choerodon-ui/pro/lib/table/style";
import _Table from "@hzero-front-ui/c7n-ui/lib/TablePro";
import React, { useMemo, useState, useCallback } from 'react';
import { FormattedMessage } from 'react-intl';
import { observer } from 'mobx-react-lite';
import { Action } from '@choerodon/boot';
import { keys } from 'lodash';
import MouserOverWrapper from "../../../../../components/MouseOverWrapper/MouserOverWrapper";
import TimePopover from "../../../../../components/timePopover/TimePopover";
import { useResourceStore } from "../../../stores";
import { useKeyValueStore } from "./stores";
import Modals from "./modals";
import KeyValueModal from "../application/modals/key-value/KeyValueProIndex";
import { useMainStore } from "../../stores";
import ResourceListTitle from "../../components/resource-list-title";
import StatusIcon from "../../../../../components/StatusIcon";
import "./index.less";
var Column = _Table.Column;

var modalKey = _Modal.key();

var modalStyle = {
  width: 'calc(100vw - 3.52rem)'
};
var ConfigMap = observer(function (props) {
  var _useResourceStore = useResourceStore(),
      prefixCls = _useResourceStore.prefixCls,
      intlPrefix = _useResourceStore.intlPrefix,
      parentId = _useResourceStore.resourceStore.getSelectedMenu.parentId,
      treeDs = _useResourceStore.treeDs;

  var _useKeyValueStore = useKeyValueStore(),
      formatMessage = _useKeyValueStore.intl.formatMessage,
      permissions = _useKeyValueStore.permissions,
      formStore = _useKeyValueStore.formStore,
      SecretTableDs = _useKeyValueStore.SecretTableDs;

  var _useMainStore = useMainStore(),
      openDeleteModal = _useMainStore.mainStore.openDeleteModal;

  function refresh() {
    treeDs.query();
    SecretTableDs.query();
  }

  function getEnvIsNotRunning() {
    var envRecord = treeDs.find(function (record) {
      return record.get('key') === parentId;
    });
    var connect = envRecord.get('connect');
    return !connect;
  }

  function renderName(_ref) {
    var value = _ref.value,
        record = _ref.record;
    var commandStatus = record.get('commandStatus');
    var disabled = getEnvIsNotRunning() || commandStatus === 'operating';
    var error = record.get('error');
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-keyValue-name")
    }, /*#__PURE__*/React.createElement(StatusIcon, {
      width: 0.4,
      name: value,
      status: commandStatus || '',
      error: error || ''
    }));
  }

  function renderValue(_ref2) {
    var _ref2$value = _ref2.value,
        value = _ref2$value === void 0 ? [] : _ref2$value;
    var keyArr = keys(value);
    return /*#__PURE__*/React.createElement(MouserOverWrapper, {
      width: 0.5
    }, keyArr && keyArr.join(','));
  }

  function renderDate(_ref3) {
    var value = _ref3.value;
    return /*#__PURE__*/React.createElement(TimePopover, {
      content: value
    });
  }

  function renderAction(_ref4) {
    var record = _ref4.record;
    var commandStatus = record.get('commandStatus');
    var disabled = getEnvIsNotRunning() || commandStatus === 'operating';

    if (disabled) {
      return null;
    }

    var id = record.get('id');
    var name = record.get('name');
    var buttons = [{
      service: permissions.edit,
      text: formatMessage({
        id: 'edit'
      }),
      action: openModal
    }, {
      service: permissions["delete"],
      text: formatMessage({
        id: 'delete'
      }),
      action: function action() {
        return openDeleteModal(parentId, id, name, 'secret', refresh);
      }
    }];
    return /*#__PURE__*/React.createElement(Action, {
      data: buttons
    });
  }

  function openModal() {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".cipher.edit")
      }),
      children: /*#__PURE__*/React.createElement(KeyValueModal, {
        title: "cipher",
        id: SecretTableDs.current.get('id'),
        envId: parentId,
        store: formStore,
        intlPrefix: intlPrefix,
        refresh: refresh
      }),
      okText: formatMessage({
        id: 'save'
      })
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-keyValue-table")
  }, /*#__PURE__*/React.createElement(Modals, null), /*#__PURE__*/React.createElement(ResourceListTitle, {
    type: "secrets"
  }), /*#__PURE__*/React.createElement(_Table, {
    dataSet: SecretTableDs,
    border: false,
    queryBar: "bar"
  }, /*#__PURE__*/React.createElement(Column, {
    name: "name",
    sortable: true,
    header: formatMessage({
      id: "".concat(intlPrefix, ".cipher")
    }),
    renderer: renderName
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderAction,
    width: 60
  }), /*#__PURE__*/React.createElement(Column, {
    name: "value",
    renderer: renderValue,
    header: formatMessage({
      id: 'key'
    })
  }), /*#__PURE__*/React.createElement(Column, {
    name: "lastUpdateDate",
    sortable: true,
    renderer: renderDate,
    width: 105
  })));
});
export default ConfigMap;