import "choerodon-ui/pro/lib/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
import "choerodon-ui/pro/lib/icon/style";
import _Icon from "choerodon-ui/pro/lib/icon";
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 } from 'react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { Action, Choerodon } from '@choerodon/boot';
import AppName from "../../../../../components/appName";
import { handlePromptError } from "../../../../../utils";
import eventStopProp from "../../../../../utils/eventStopProp";
import { useResourceStore } from "../../../stores";
import { useTreeItemStore } from "./stores";

var modalKey = _Modal.key();

function AppItem(_ref) {
  var name = _ref.name,
      record = _ref.record,
      formatMessage = _ref.intl.formatMessage,
      intlPrefix = _ref.intlPrefix;

  var _useResourceStore = useResourceStore(),
      treeDs = _useResourceStore.treeDs,
      id = _useResourceStore.AppState.currentMenuType.id;

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

  function handleClick() {
    return _handleClick.apply(this, arguments);
  }

  function _handleClick() {
    _handleClick = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var envId, appId, result;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (record) {
                _context.next = 2;
                break;
              }

              return _context.abrupt("return");

            case 2:
              envId = record.get('parentId');
              appId = record.get('id');
              _context.prev = 4;
              _context.next = 7;
              return treeItemStore.removeService(id, envId, [appId]);

            case 7:
              result = _context.sent;

              if (handlePromptError(result, false)) {
                treeDs.query();
              }

              _context.next = 14;
              break;

            case 11:
              _context.prev = 11;
              _context.t0 = _context["catch"](4);
              Choerodon.handleResponseError(_context.t0);

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[4, 11]]);
    }));
    return _handleClick.apply(this, arguments);
  }

  function openModal() {
    _Modal.open({
      movable: false,
      closable: false,
      key: modalKey,
      title: formatMessage({
        id: "".concat(intlPrefix, ".modal.service.delete")
      }),
      children: formatMessage({
        id: "".concat(intlPrefix, ".modal.service.delete.desc")
      }),
      okText: formatMessage({
        id: 'delete'
      }),
      onOk: handleClick,
      okProps: {
        color: 'red'
      },
      cancelProps: {
        color: 'dark'
      }
    });
  }

  function getSuffix() {
    var actionData = [{
      service: ['choerodon.code.project.deploy.app-deployment.resource.ps.delete-contact'],
      text: formatMessage({
        id: "".concat(intlPrefix, ".modal.service.delete")
      }),
      action: openModal
    }];
    return /*#__PURE__*/React.createElement(Action, {
      placement: "bottomRight",
      data: actionData,
      onClick: eventStopProp
    });
  }

  function renderIcon() {
    var type = record.get('type');
    var iconType = 'widgets';
    var message = 'project';

    if (['middleware_service', 'market_service'].includes(type)) {
      iconType = 'application_market';
      message = 'market';
    } else if (type === 'share_service') {
      iconType = 'share';
      message = 'share';
    }

    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: formatMessage({
        id: message
      })
    }, /*#__PURE__*/React.createElement(_Icon, {
      type: iconType
    }));
  }

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

export default injectIntl(observer(AppItem));