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 } from 'react';
import { observer } from 'mobx-react-lite';
import { Action } from '@choerodon/boot';
import MouserOverWrapper from "../../../../../../components/MouseOverWrapper/MouserOverWrapper";
import StatusTags from "../../../../../../components/status-tag";
import TimePopover from "../../../../../../components/timePopover/TimePopover";
import KeyValueModal from "../modals/key-value/KeyValueProIndex";
import { useResourceStore } from "../../../../stores";
import { useApplicationStore } from "../stores";
import ClickText from "../../../../../../components/click-text";
import { useMainStore } from "../../../stores";
import "./index.less";
import StatusIcon from "../../../../../../components/StatusIcon";
var Column = _Table.Column;

var modalKey = _Modal.key();

var modalStyle = {
  width: 'calc(100vw - 3.52rem)'
};
var AppConfigs = observer(function () {
  var _useResourceStore = useResourceStore(),
      formatMessage = _useResourceStore.intl.formatMessage,
      prefixCls = _useResourceStore.prefixCls,
      intlPrefix = _useResourceStore.intlPrefix,
      _useResourceStore$res = _useResourceStore.resourceStore.getSelectedMenu,
      id = _useResourceStore$res.id,
      parentId = _useResourceStore$res.parentId,
      treeDs = _useResourceStore.treeDs;

  var _useApplicationStore = useApplicationStore(),
      mappingStore = _useApplicationStore.mappingStore,
      mappingDs = _useApplicationStore.mappingDs;

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

  var statusStyle = useMemo(function () {
    return {
      marginRight: '0.08rem'
    };
  }, []);

  function refresh() {
    treeDs.query();
    return mappingDs.query();
  }

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

  function handleEdit(record) {
    _Modal.open({
      key: modalKey,
      style: modalStyle,
      drawer: true,
      title: formatMessage({
        id: "".concat(intlPrefix, ".configMap.edit")
      }),
      children: /*#__PURE__*/React.createElement(KeyValueModal, {
        modeSwitch: true,
        intlPrefix: intlPrefix,
        title: "mapping",
        id: record.get('id'),
        envId: parentId,
        appId: id,
        store: mappingStore,
        refresh: refresh
      }),
      okText: formatMessage({
        id: 'save'
      })
    });
  }

  function renderName(_ref) {
    var value = _ref.value,
        record = _ref.record;
    var commandStatus = record.get('commandStatus');
    var error = record.get('error');
    var disabled = getEnvIsNotRunning() || commandStatus === 'operating';
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(prefixCls, "-keyValue-name")
    }, /*#__PURE__*/React.createElement(StatusIcon, {
      width: 0.4,
      name: value,
      clickAble: !disabled,
      onClick: handleEdit,
      status: commandStatus || '',
      error: error || '',
      permissionCode: ['choerodon.code.project.deploy.app-deployment.resource.ps.edit-configmap'],
      record: record
    }));
  }

  function renderKey(_ref2) {
    var _ref2$value = _ref2.value,
        value = _ref2$value === void 0 ? [] : _ref2$value;
    return /*#__PURE__*/React.createElement(MouserOverWrapper, {
      width: 0.5
    }, value.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 configId = record.get('id');
    var name = record.get('name');
    var disabled = getEnvIsNotRunning() || commandStatus === 'operating';

    if (disabled) {
      return null;
    }

    var buttons = [{
      service: ['choerodon.code.project.deploy.app-deployment.resource.ps.delete-map'],
      text: formatMessage({
        id: 'delete'
      }),
      action: function action() {
        return openDeleteModal(parentId, configId, name, 'configMap', refresh);
      }
    }];
    return /*#__PURE__*/React.createElement(Action, {
      data: buttons
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-mapping-content")
  }, /*#__PURE__*/React.createElement("div", {
    className: "c7ncd-tab-table"
  }, /*#__PURE__*/React.createElement(_Table, {
    dataSet: mappingDs,
    border: false,
    queryBar: "bar"
  }, /*#__PURE__*/React.createElement(Column, {
    name: "name",
    sortable: true,
    header: formatMessage({
      id: "".concat(intlPrefix, ".application.tabs.mapping")
    }),
    renderer: renderName
  }), /*#__PURE__*/React.createElement(Column, {
    renderer: renderAction,
    width: "0.7rem"
  }), /*#__PURE__*/React.createElement(Column, {
    name: "key",
    renderer: renderKey
  }), /*#__PURE__*/React.createElement(Column, {
    name: "lastUpdateDate",
    renderer: renderDate,
    width: 105,
    sortable: true
  }))));
});
export default AppConfigs;