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/tooltip/style";
import _Tooltip from "choerodon-ui/pro/lib/tooltip";
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 } from 'react';
import { observer } from 'mobx-react-lite';
import { Choerodon } from '@choerodon/boot';
import { map, some, compact } from 'lodash';
import DynamicSelect from "../../../../../../../components/dynamic-select-new";
import { handlePromptError } from "../../../../../../../utils";
import "./index.less";
var Option = _Select.Option;
var Permission = observer(function (props) {
  var refreshPermission = props.refreshPermission,
      modal = props.modal,
      onOk = props.onOk,
      intlPrefix = props.intlPrefix,
      prefixCls = props.prefixCls,
      formatMessage = props.formatMessage,
      clusterDetail = props.clusterDetail,
      PermissionDs = props.PermissionDs,
      NonPermissionDs = props.NonPermissionDs;
  useEffect(function () {
    return function () {
      clusterDetail.reset();
    };
  }, []);
  modal.handleOk( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
    var projectIds, skipCheckProjectPermission, projects, res;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            projectIds = map(PermissionDs.created, function (createdRecord) {
              return createdRecord.get('projectId');
            }) || [];
            skipCheckProjectPermission = clusterDetail.get('skipCheckProjectPermission');
            projects = {
              projectIds: compact(projectIds),
              skipCheckProjectPermission: skipCheckProjectPermission
            };

            if (projects && projects.projectIds) {
              _context.next = 5;
              break;
            }

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

          case 5:
            _context.prev = 5;
            _context.next = 8;
            return onOk(projects);

          case 8:
            res = _context.sent;

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

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

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

          case 15:
            _context.next = 21;
            break;

          case 17:
            _context.prev = 17;
            _context.t0 = _context["catch"](5);
            Choerodon.handleResponseError(_context.t0);
            return _context.abrupt("return", false);

          case 21:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[5, 17]]);
  })));

  function handleFilter(optionRecord) {
    var flag = some(PermissionDs.created, function (creatRecord) {
      return creatRecord.get('projectId') === optionRecord.get('id');
    });
    return !flag;
  }

  function renderOption(_ref2) {
    var optionRecord = _ref2.record;
    var code = optionRecord.get('code');
    var name = optionRecord.get('name');
    return /*#__PURE__*/React.createElement(_Tooltip, {
      title: code
    }, name);
  }

  return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefixCls, "-modal-head")
  }, formatMessage({
    id: "".concat(intlPrefix, ".visibility")
  })), /*#__PURE__*/React.createElement(_Form, {
    record: clusterDetail
  }, /*#__PURE__*/React.createElement(_SelectBox, {
    name: "skipCheckProjectPermission"
  }, /*#__PURE__*/React.createElement(Option, {
    value: true
  }, formatMessage({
    id: "".concat(intlPrefix, ".project.all")
  })), /*#__PURE__*/React.createElement(Option, {
    value: false
  }, formatMessage({
    id: "".concat(intlPrefix, ".project.part")
  })))), !clusterDetail.get('skipCheckProjectPermission') && /*#__PURE__*/React.createElement(DynamicSelect, {
    selectDataSet: PermissionDs,
    optionsDataSet: NonPermissionDs,
    optionsFilter: handleFilter,
    optionsRenderer: renderOption,
    selectName: "projectId",
    optionKeyName: "id",
    addText: formatMessage({
      id: "".concat(intlPrefix, ".add.project")
    })
  }));
});
export default Permission;