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

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, useEffect, useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import map from 'lodash/map';
import compact from 'lodash/compact';
import DynamicSelect from "../../../../../components/dynamic-select-new";
import UserInfo from "../../../../../components/userInfo";
import "./index.less";
var Option = _Select.Option;
export default observer(function (props) {
  var dataSet = props.dataSet,
      nonePermissionDs = props.nonePermissionDs,
      refresh = props.refresh,
      baseDs = props.baseDs,
      store = props.store,
      projectId = props.projectId,
      formatMessage = props.formatMessage,
      prefixCls = props.prefixCls,
      intlPrefix = props.intlPrefix,
      modal = props.modal;
  var record = useMemo(function () {
    return baseDs.current;
  }, [baseDs.current]);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var data;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            if (!record.get('skipCheckPermission')) {
              _context.next = 17;
              break;
            }

            _context.prev = 1;
            data = {
              skipCheckPermission: true,
              userIds: []
            };
            _context.next = 5;
            return store.updatePermission(projectId, record.get('id'), data);

          case 5:
            if (!_context.sent) {
              _context.next = 9;
              break;
            }

            refresh();
            _context.next = 10;
            break;

          case 9:
            return _context.abrupt("return", false);

          case 10:
            _context.next = 15;
            break;

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

          case 15:
            _context.next = 32;
            break;

          case 17:
            dataSet.transport.create = function (_ref2) {
              var data = _ref2.data;
              var res = {
                skipCheckPermission: false,
                userIds: compact(map(data, 'iamUserId') || [])
              };
              return {
                url: "/devops/v1/projects/".concat(projectId, "/app_service/").concat(record.get('id'), "/update_permission"),
                method: 'post',
                data: res
              };
            };

            _context.prev = 18;
            _context.next = 21;
            return dataSet.submit();

          case 21:
            _context.t1 = _context.sent;

            if (!(_context.t1 !== false)) {
              _context.next = 26;
              break;
            }

            refresh();
            _context.next = 27;
            break;

          case 26:
            return _context.abrupt("return", false);

          case 27:
            _context.next = 32;
            break;

          case 29:
            _context.prev = 29;
            _context.t2 = _context["catch"](18);
            return _context.abrupt("return", false);

          case 32:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[1, 12], [18, 29]]);
  })));
  modal.handleCancel(function () {
    record.reset();
    dataSet.reset();
  });

  function renderUserOption(_ref3) {
    var optionRecord = _ref3.record;
    return /*#__PURE__*/React.createElement(UserInfo, {
      name: optionRecord.get('realName') || '',
      id: record.get('loginName')
    });
  }

  function renderer(_ref4) {
    var optionRecord = _ref4.optionRecord;
    return /*#__PURE__*/React.createElement(UserInfo, {
      name: optionRecord.get('realName') || '',
      id: record.get('loginName')
    });
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(_Form, {
    record: record
  }, /*#__PURE__*/React.createElement(_SelectBox, {
    name: "skipCheckPermission"
  }, /*#__PURE__*/React.createElement(Option, {
    value: true
  }, formatMessage({
    id: "".concat(intlPrefix, ".member.all")
  })), /*#__PURE__*/React.createElement(Option, {
    value: false
  }, formatMessage({
    id: "".concat(intlPrefix, ".member.specific")
  })))), record && !record.get('skipCheckPermission') && /*#__PURE__*/React.createElement(DynamicSelect, {
    selectDataSet: dataSet,
    optionsRenderer: renderUserOption,
    optionsDataSet: nonePermissionDs,
    renderer: renderer,
    selectName: "iamUserId",
    addText: formatMessage({
      id: "".concat(intlPrefix, ".add.member")
    })
  }));
});