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, { useCallback } from 'react';
import { observer } from 'mobx-react-lite';
import { Page, Header, Breadcrumb, Content, HeaderButtons } from '@choerodon/boot';
import map from 'lodash/map';
import ContentHeader from "./components/content-header";
import ContentList from "./components/content-list";
import CreateHost from "./components/create-host";
import HostConfigApis from "./apis";
import { useHostConfigStore } from "./stores";
var HostConfig = observer(function () {
  var _useHostConfigStore = useHostConfigStore(),
      prefixCls = _useHostConfigStore.prefixCls,
      intlPrefix = _useHostConfigStore.intlPrefix,
      formatMessage = _useHostConfigStore.formatMessage,
      refresh = _useHostConfigStore.refresh,
      listDs = _useHostConfigStore.listDs,
      projectId = _useHostConfigStore.projectId,
      mainStore = _useHostConfigStore.mainStore,
      searchDs = _useHostConfigStore.searchDs;

  var afterCreate = useCallback(function (selectedTabKey) {
    if (selectedTabKey && selectedTabKey !== mainStore.getCurrentTabKey) {
      searchDs.reset();
      listDs.setQueryParameter('type', selectedTabKey);
      listDs.setQueryParameter('params', '');
      listDs.setQueryParameter('status', '');
      mainStore.setCurrentTabKey(selectedTabKey);
    }

    refresh();
  }, [mainStore.getCurrentTabKey]);

  var handleCorrect = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var postData, res;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.prev = 0;
              postData = map(listDs.toData(), 'id');
              _context.next = 4;
              return HostConfigApis.batchCorrect(projectId, postData, mainStore.getCurrentTabKey);

            case 4:
              res = _context.sent;

              if (!(res && res.failed)) {
                _context.next = 7;
                break;
              }

              return _context.abrupt("return", false);

            case 7:
              refresh();
              return _context.abrupt("return", true);

            case 11:
              _context.prev = 11;
              _context.t0 = _context["catch"](0);
              return _context.abrupt("return", false);

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, null, [[0, 11]]);
    }));

    return function handleCorrect() {
      return _ref.apply(this, arguments);
    };
  }();

  var handleAdjustment = function handleAdjustment() {
    _Modal.open({
      key: _Modal.key(),
      title: formatMessage({
        id: "".concat(intlPrefix, ".batch.correct.title")
      }),
      children: formatMessage({
        id: "".concat(intlPrefix, ".batch.correct.des")
      }),
      movable: false,
      okText: formatMessage({
        id: "".concat(intlPrefix, ".batch.correct")
      }),
      onOk: handleCorrect
    });
  };

  var handleAdd = function handleAdd() {
    _Modal.open({
      key: _Modal.key(),
      title: formatMessage({
        id: "".concat(intlPrefix, ".add")
      }),
      style: {
        width: 380
      },
      drawer: true,
      children: /*#__PURE__*/React.createElement(CreateHost, {
        refresh: afterCreate
      }),
      okText: formatMessage({
        id: 'create'
      })
    });
  };

  return /*#__PURE__*/React.createElement(Page, {
    service: ['choerodon.code.project.deploy.host.ps.default']
  }, /*#__PURE__*/React.createElement(Header, null, /*#__PURE__*/React.createElement(HeaderButtons, {
    items: [{
      name: formatMessage({
        id: "".concat(intlPrefix, ".add")
      }),
      icon: 'playlist_add',
      display: true,
      permissions: ['choerodon.code.project.deploy.host.ps.create'],
      handler: handleAdd
    }, {
      name: formatMessage({
        id: "".concat(intlPrefix, ".adjustment")
      }),
      icon: 'refresh',
      display: true,
      permissions: ['choerodon.code.project.deploy.host.ps.correct'],
      handler: handleAdjustment
    }, {
      icon: 'refresh',
      display: true,
      handler: refresh
    }],
    showClassName: false
  })), /*#__PURE__*/React.createElement(Breadcrumb, null), /*#__PURE__*/React.createElement(Content, {
    className: "".concat(prefixCls, "-content")
  }, /*#__PURE__*/React.createElement(ContentHeader, null), /*#__PURE__*/React.createElement(ContentList, null)));
});
export default HostConfig;