import "choerodon-ui/pro/lib/table/style";
import _Table from "@hzero-front-ui/c7n-ui/lib/TablePro";
import React from 'react';
import { Action } from '@choerodon/boot';
import map from 'lodash/map';
import TimePopover from "../../../../../components/time-popover";
import { useEnvironmentStore } from "../../../stores";
import { useDetailStore } from "./stores";
import { isNotRunning } from "../../../util";
var Column = _Table.Column;
export default function Permissions() {
  var _useEnvironmentStore = useEnvironmentStore(),
      getSelectedMenu = _useEnvironmentStore.envStore.getSelectedMenu;

  var _useDetailStore = useDetailStore(),
      formatMessage = _useDetailStore.intl.formatMessage,
      tableDs = _useDetailStore.permissionsDs,
      intlPrefix = _useDetailStore.intlPrefix;

  function handleDelete() {
    var record = tableDs.current;
    var modalProps = {
      title: formatMessage({
        id: "".concat(intlPrefix, ".permission.delete.title")
      }),
      children: formatMessage({
        id: "".concat(intlPrefix, ".permission.delete.des")
      }),
      okText: formatMessage({
        id: 'delete'
      }),
      okProps: {
        color: 'red'
      },
      cancelProps: {
        color: 'dark'
      }
    };
    tableDs["delete"](record, modalProps);
  }

  function renderActions(_ref) {
    var record = _ref.record;
    var skipCheckPermission = getSelectedMenu.skipCheckPermission;
    var isOwner = record.get('gitlabProjectOwner');
    var actionData = [{
      service: [],
      text: formatMessage({
        id: 'delete'
      }),
      action: handleDelete
    }];
    var displayAction = !isOwner && !skipCheckPermission;
    return displayAction ? /*#__PURE__*/React.createElement(Action, {
      data: actionData
    }) : null;
  }

  function renderDate(_ref2) {
    var value = _ref2.value;
    return value ? /*#__PURE__*/React.createElement(TimePopover, {
      datetime: value
    }) : null;
  }

  function renderRole(_ref3) {
    var value = _ref3.value;
    var roles = map(value || [], 'name');
    return roles.join();
  }

  function getActionColumn() {
    var disabled = isNotRunning(getSelectedMenu);
    return !disabled && /*#__PURE__*/React.createElement(Column, {
      renderer: renderActions
    });
  }

  return /*#__PURE__*/React.createElement("div", {
    className: "c7ncd-tab-table"
  }, /*#__PURE__*/React.createElement(_Table, {
    dataSet: tableDs,
    border: false,
    queryBar: "bar",
    pristine: true
  }, /*#__PURE__*/React.createElement(Column, {
    name: "realName",
    sortable: true
  }), getActionColumn(), /*#__PURE__*/React.createElement(Column, {
    name: "loginName",
    sortable: true
  }), /*#__PURE__*/React.createElement(Column, {
    name: "roles",
    renderer: renderRole
  }), /*#__PURE__*/React.createElement(Column, {
    name: "creationDate",
    renderer: renderDate,
    sortable: true
  })));
}