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 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, { Fragment, useMemo } from 'react';
import { observer } from 'mobx-react-lite';
import map from 'lodash/map';
import { handlePromptError } from "../../../../../../../utils";
import DynamicSelect from "../../../../../../../components/dynamic-select-new";
import UserInfo from "../../../../../../../components/userInfo";
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 skipCheckPermission, baseData, res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            skipCheckPermission = record.get('skipCheckPermission');
            baseData = {
              envId: record.get('id'),
              objectVersionNumber: record.get('objectVersionNumber'),
              skipCheckPermission: skipCheckPermission
            };

            if (!skipCheckPermission) {
              _context.next = 12;
              break;
            }

            _context.next = 5;
            return store.addUsers(_objectSpread({
              projectId: projectId,
              userIds: []
            }, baseData));

          case 5:
            res = _context.sent;

            if (!handlePromptError(res, false)) {
              _context.next = 11;
              break;
            }

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

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

          case 12:
            dataSet.transport.create = function (_ref2) {
              var data = _ref2.data;

              var res = _objectSpread({
                userIds: map(data, 'iamUserId')
              }, baseData);

              return {
                url: "/devops/v1/projects/".concat(projectId, "/envs/").concat(record.get('id'), "/permission"),
                method: 'post',
                data: res
              };
            };

            _context.prev = 13;
            _context.next = 16;
            return dataSet.submit();

          case 16:
            _context.t0 = _context.sent;

            if (!(_context.t0 !== false)) {
              _context.next = 21;
              break;
            }

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

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

          case 22:
            _context.next = 27;
            break;

          case 24:
            _context.prev = 24;
            _context.t1 = _context["catch"](13);
            return _context.abrupt("return", false);

          case 27:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[13, 24]]);
  })));
  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")
    })
  }));
});