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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

import React, { Fragment, useMemo, useState } from 'react';
import { observer } from 'mobx-react-lite';
import { injectIntl } from 'react-intl';
import { Action } from '@choerodon/boot';
import { useResourceStore } from "../../../stores";
import { useMainStore } from "../../stores";
import eventStopProp from "../../../../../utils/eventStopProp";
import openWarnModal from "../../../../../utils/openWarnModal";
import DomainForm from "../../components/domain-form";

var modalKey = _Modal.key();

var modalStyle = {
  width: 740
};

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

  var _useResourceStore = useResourceStore(),
      treeDs = _useResourceStore.treeDs,
      _useResourceStore$res = _useResourceStore.resourceStore,
      _useResourceStore$res2 = _useResourceStore$res.getSelectedMenu,
      itemType = _useResourceStore$res2.itemType,
      parentId = _useResourceStore$res2.parentId,
      setUpTarget = _useResourceStore$res.setUpTarget,
      checkExist = _useResourceStore$res.checkExist,
      _useResourceStore$ite = _useResourceStore.itemTypes,
      INGRESS_GROUP = _useResourceStore$ite.INGRESS_GROUP,
      INGRESS_ITEM = _useResourceStore$ite.INGRESS_ITEM,
      projectId = _useResourceStore.AppState.currentMenuType.projectId;

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

  function freshTree() {
    treeDs.query();
  }

  function freshMenu() {
    freshTree();

    var _record$get$split = record.get('parentId').split('**'),
        _record$get$split2 = _slicedToArray(_record$get$split, 1),
        envId = _record$get$split2[0];

    if (itemType === INGRESS_GROUP && envId === parentId) {
      setUpTarget({
        type: INGRESS_GROUP,
        id: parentId
      });
    } else {
      setUpTarget({
        type: INGRESS_ITEM,
        id: record.get('id')
      });
    }
  }

  function getEnvIsNotRunning() {
    var _record$get$split3 = record.get('parentId').split('**'),
        _record$get$split4 = _slicedToArray(_record$get$split3, 1),
        envId = _record$get$split4[0];

    var envRecord = treeDs.find(function (item) {
      return item.get('key') === envId;
    });
    var connect = envRecord.get('connect');
    return !connect;
  }

  function checkDataExist() {
    return checkExist({
      projectId: projectId,
      type: 'ingress',
      envId: record.get('parentId').split('**')[0],
      id: record.get('id')
    }).then(function (isExist) {
      if (!isExist) {
        openWarnModal(freshTree, formatMessage);
      }

      return isExist;
    });
  }

  function openModal() {
    checkDataExist().then(function (query) {
      if (query) {
        _Modal.open({
          key: modalKey,
          style: modalStyle,
          drawer: true,
          title: formatMessage({
            id: 'domain.update.head'
          }),
          children: /*#__PURE__*/React.createElement(DomainForm, {
            envId: record.get('parentId').split('**')[0],
            ingressId: record.get('id'),
            refresh: freshMenu,
            intlPrefix: intlPrefix,
            prefixCls: "c7ncd-deployment"
          }),
          okText: formatMessage({
            id: 'save'
          })
        });
      }
    });
  }

  function getSuffix() {
    var id = record.get('id');
    var ingressName = record.get('name');

    var _record$get$split5 = record.get('parentId').split('**'),
        _record$get$split6 = _slicedToArray(_record$get$split5, 1),
        envId = _record$get$split6[0];

    var status = record.get('status');
    var disabled = getEnvIsNotRunning() || status === 'operating';

    if (disabled) {
      return null;
    }

    var actionData = [{
      service: ['choerodon.code.project.deploy.app-deployment.resource.ps.update.domain'],
      text: formatMessage({
        id: 'edit'
      }),
      action: openModal
    }, {
      service: ['choerodon.code.project.deploy.app-deployment.resource.ps.delete-domain'],
      text: formatMessage({
        id: 'delete'
      }),
      action: function action() {
        return openDeleteModal(envId, id, ingressName, 'ingress', freshMenu);
      }
    }];
    return /*#__PURE__*/React.createElement(Action, {
      placement: "bottomRight",
      data: actionData,
      onClick: eventStopProp
    });
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(_Icon, {
    type: "language"
  }), name, getSuffix());
}

export default injectIntl(observer(IngressItem));