import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

import React, { Fragment, useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { Action } from '@choerodon/boot';
import TreeItemName from "../../../../../components/treeitem-name";
import GroupForm from "../../modals/GroupForm";
import { handlePromptError } from "../../../../../utils";
import eventStopProp from "../../../../../utils/eventStopProp";
import { useEnvironmentStore } from "../../../stores";
import { useTreeItemStore } from "./stores";

var modalKey = _Modal.key();

var confirmKey = _Modal.key();

function GroupItem(_ref) {
  var record = _ref.record,
      search = _ref.search,
      formatMessage = _ref.intl.formatMessage,
      intlPrefix = _ref.intlPrefix;
  var modalStyle = useMemo(function () {
    return {
      width: 380
    };
  }, []);

  var _useEnvironmentStore = useEnvironmentStore(),
      treeDs = _useEnvironmentStore.treeDs,
      envStore = _useEnvironmentStore.envStore,
      id = _useEnvironmentStore.AppState.currentMenuType.id;

  var _useTreeItemStore = useTreeItemStore(),
      groupFormDs = _useTreeItemStore.groupFormDs;

  function handleClick() {
    var groupId = record.get('id');
    var name = record.get('name');
    var current = groupFormDs.current;
    current.set('name', name);
    current.set('id', groupId);

    _Modal.open({
      key: modalKey,
      title: formatMessage({
        id: "".concat(intlPrefix, ".group.edit")
      }),
      children: /*#__PURE__*/React.createElement(GroupForm, {
        dataSet: groupFormDs,
        treeDs: treeDs
      }),
      drawer: true,
      style: modalStyle,
      okText: formatMessage({
        id: 'save'
      })
    });
  }

  function handleDelete() {
    return _handleDelete.apply(this, arguments);
  }

  function _handleDelete() {
    _handleDelete = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var groupId, result;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              groupId = record.get('id');
              _context.prev = 1;
              _context.next = 4;
              return envStore.deleteGroup(id, groupId);

            case 4:
              result = _context.sent;
              handlePromptError(result, false);

            case 6:
              _context.prev = 6;
              treeDs.query();
              return _context.finish(6);

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[1,, 6, 9]]);
    }));
    return _handleDelete.apply(this, arguments);
  }

  function confirmDelete() {
    var name = record.get('name');

    _Modal.open({
      movable: false,
      closable: false,
      header: true,
      key: confirmKey,
      title: formatMessage({
        id: "".concat(intlPrefix, ".group.delete")
      }, {
        name: name
      }),
      children: /*#__PURE__*/React.createElement("div", null, formatMessage({
        id: "".concat(intlPrefix, ".group.delete.warn")
      })),
      onOk: handleDelete,
      okText: formatMessage({
        id: 'delete'
      }),
      okProps: {
        color: 'red'
      },
      cancelProps: {
        color: 'dark'
      }
    });
  }

  function getName() {
    var itemName = record.get('name');
    return /*#__PURE__*/React.createElement(TreeItemName, {
      headSpace: false,
      name: itemName,
      search: search
    });
  }

  function getSuffix() {
    var groupId = record.get('id');
    if (!groupId) return null;
    var actionData = [{
      service: ['choerodon.code.project.deploy.environment.ps.group-modify'],
      text: formatMessage({
        id: "".concat(intlPrefix, ".modal.group.modify")
      }),
      action: handleClick
    }, {
      service: ['choerodon.code.project.deploy.environment.ps.group-delete'],
      text: formatMessage({
        id: "".concat(intlPrefix, ".modal.group.delete")
      }),
      action: confirmDelete
    }];
    return /*#__PURE__*/React.createElement(Action, {
      placement: "bottomRight",
      data: actionData,
      onClick: eventStopProp
    });
  }

  return /*#__PURE__*/React.createElement(Fragment, null, getName(), getSuffix());
}

export default injectIntl(observer(GroupItem));