import "choerodon-ui/pro/lib/form/style";
import _Form from "@hzero-front-ui/c7n-ui/lib/FormPro";
import "choerodon-ui/pro/lib/select/style";
import _Select from "choerodon-ui/pro/lib/select";
import "choerodon-ui/pro/lib/modal/style";
import _Modal from "choerodon-ui/pro/lib/modal";

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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

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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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, { useEffect } from 'react';
import { observer } from 'mobx-react-lite';
import { axios } from '@choerodon/boot';
import PipelineCreate from "../../../PipelineCreate";
export default observer(function (_ref) {
  var ds = _ref.ds,
      modal = _ref.modal,
      projectId = _ref.projectId,
      editBlockStore = _ref.editBlockStore,
      record = _ref.record,
      handleRefresh = _ref.handleRefresh,
      seletDs = _ref.seletDs;
  useEffect(function () {
    ds.reset();
    seletDs.query();
    return function () {
      seletDs.reset();
    };
  }, []);
  useEffect(function () {
    modal.update({
      okProps: {
        disabled: !ds.current.get('appServiceId')
      }
    });
  }, [ds.current.get('appServiceId')]);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var oldMainData, result, appServiceId;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            oldMainData = JSON.parse(JSON.stringify(editBlockStore.getMainData));
            _context.next = 3;
            return editBlockStore.loadDetail(projectId, record.get('id'));

          case 3:
            result = _context.sent;
            editBlockStore.setMainData(_objectSpread(_objectSpread({}, result), {}, {
              devopsCdStageVOS: [],
              name: undefined,
              appServiceId: ds.current.get('appServiceId'),
              appServiceName: seletDs.toData().find(function (item) {
                return item.appServiceId === ds.current.get('appServiceId');
              }).appServiceName
            }));
            editBlockStore.setStepData(_toConsumableArray(result.devopsCiStageVOS));
            appServiceId = ds.current.get('appServiceId');

            _Modal.open({
              key: _Modal.key(),
              title: '创建流水线',
              style: {
                width: 'calc(100vw - 3.52rem)'
              },
              drawer: true,
              children: /*#__PURE__*/React.createElement(PipelineCreate, {
                appService: {
                  id: appServiceId,
                  name: seletDs.toData().find(function (item) {
                    return item.appServiceId === appServiceId;
                  }).appServiceName
                },
                oldMainData: oldMainData,
                dataSource: editBlockStore.getMainData,
                refreshTree: handleRefresh,
                editBlockStore: editBlockStore
              }),
              okText: '创建'
            });

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

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));

  var renderer = function renderer(_ref3) {
    var text = _ref3.text;
    return text;
  };

  var optionRenderer = function optionRenderer(_ref4) {
    var text = _ref4.text;
    return text === '加载更多' ? /*#__PURE__*/React.createElement("a", {
      role: "none",
      style: {
        width: '100%',
        height: '100%',
        display: 'block'
      },
      onClick: handleClickMore
    }, text) : text;
  };

  var handleClickMore = /*#__PURE__*/function () {
    var _ref5 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(e) {
      var pageSize, result;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              e.stopPropagation();
              pageSize = seletDs.pageSize + 20;
              _context2.next = 4;
              return axios.post("/devops/v1/projects/".concat(projectId, "/app_service/page_app_services_without_ci?page=0&size=").concat(pageSize));

            case 4:
              result = _context2.sent;

              if (result.length % 20 === 0) {
                result.push({
                  appServiceId: 'more',
                  appServiceName: '加载更多'
                });
              }

              seletDs.current.set('pageSize', pageSize);
              seletDs.loadData(result);

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));

    return function handleClickMore(_x) {
      return _ref5.apply(this, arguments);
    };
  }();

  return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("p", null, "\u8BF7\u4E3A\u65B0\u7684\u6D41\u6C34\u7EBF\u9009\u62E9\u5173\u8054\u5E94\u7528\u670D\u52A1"), /*#__PURE__*/React.createElement(_Form, {
    dataSet: ds
  }, /*#__PURE__*/React.createElement(_Select, {
    name: "appServiceId",
    searchable: true,
    searchMatcher: "appServiceName",
    optionRenderer: optionRenderer,
    renderer: renderer
  })));
});