import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
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 { FormattedMessage } from 'react-intl';
import TimePopover from "../../../../../components/time-popover";
import { useEnvironmentStore } from "./stores";
var Column = _Table.Column;
export default function Permissions() {
  var _useEnvironmentStore = useEnvironmentStore(),
      formatMessage = _useEnvironmentStore.intl.formatMessage,
      tableDs = _useEnvironmentStore.permissionsDs,
      baseInfoDs = _useEnvironmentStore.baseInfoDs,
      intlPrefix = _useEnvironmentStore.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 actionData = [{
      service: [],
      text: formatMessage({
        id: 'delete'
      }),
      action: handleDelete
    }];
    var isOwner = !record.get('gitlabProjectOwner');
    return isOwner && /*#__PURE__*/React.createElement(Action, {
      data: actionData
    });
  }

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

  function renderRole(_ref3) {
    var value = _ref3.value;
    var roles = map(value || [], 'name');
    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: roles.join()
    }, roles.join());
  }

  function getActionColumn() {
    var envRecord = baseInfoDs.current;
    if (!envRecord) return null;
    var isSkip = envRecord.get('skipCheckPermission');
    return !isSkip && /*#__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
  })));
}