"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _message = _interopRequireDefault(require("@alifd/next/lib/message"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _react = _interopRequireWildcard(require("react"));
var _locale = require("../../../src/locale");
var _coreLibBeta = require("@cverse/core-lib-beta");
var _boxSlotComponent = _interopRequireDefault(require("../_components/boxSlotComponent"));
var _RuleSetterService = require("./RuleSetterService");
var _utils = require("../../_utils/utils");
require("./index.less");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
/**
 * 主要用于转盘里获取规则内容
 * 只有分配算法规则支持  支持4-8
 * 增加支持自定义变量
 */

var CRuleSelectSetter = function CRuleSelectSetter(props) {
  var value = props.value,
    onChange = props.onChange,
    dataType = props.dataType;
  var _useState = (0, _react.useState)(),
    selectRule = _useState[0],
    setSelectRule = _useState[1]; //选中的规则
  var _useState2 = (0, _react.useState)([]),
    dataSourceSelectList = _useState2[0],
    setDataSourceSelectList = _useState2[1]; //数据源选择
  var _useState3 = (0, _react.useState)([]),
    customList = _useState3[0],
    setCustomList = _useState3[1]; //自定义变量
  var engineApi = new _coreLibBeta.EngineApi();
  var ruleSetterService = (0, _react.useRef)();

  /**
   *
   * @param item 根据ruleJSon获取规则奖项详情
   * @returns
   */
  var handleRuleJson = function handleRuleJson(item) {
    var _ruleJson$ruleList;
    //处理ruleJson
    var ruleJson = JSON.parse(item === null || item === void 0 ? void 0 : item.ruleJson);
    var prizesCount = 0;
    ruleJson === null || ruleJson === void 0 ? void 0 : (_ruleJson$ruleList = ruleJson.ruleList) === null || _ruleJson$ruleList === void 0 ? void 0 : _ruleJson$ruleList.map(function (item) {
      var _item$groupSlotIOList;
      (_item$groupSlotIOList = item.groupSlotIOList) === null || _item$groupSlotIOList === void 0 ? void 0 : _item$groupSlotIOList.map(function (ioList) {
        var _ioList$groupSlotList;
        var count = (_ioList$groupSlotList = ioList.groupSlotListOutput) === null || _ioList$groupSlotList === void 0 ? void 0 : _ioList$groupSlotList.length;
        prizesCount += count;
      });
    });
    return prizesCount;
  };
  var getPrizesCount = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(ruleId) {
      var res, count;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return engineApi.getClusterRuleDetail(ruleId);
          case 2:
            res = _context.sent;
            count = handleRuleJson(res === null || res === void 0 ? void 0 : res.data);
            return _context.abrupt("return", count);
          case 5:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function getPrizesCount(_x) {
      return _ref.apply(this, arguments);
    };
  }();

  //更新选中规则时，获取icon name
  var onChangeRule = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2(item) {
      var prizesCount, _item$list;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.prev = 0;
            prizesCount = 0;
            if (!((selectRule === null || selectRule === void 0 ? void 0 : selectRule.dataType) === _RuleSetterService.DataType.customState)) {
              _context2.next = 6;
              break;
            }
            prizesCount = item === null || item === void 0 ? void 0 : (_item$list = item.list) === null || _item$list === void 0 ? void 0 : _item$list.length;
            _context2.next = 9;
            break;
          case 6:
            _context2.next = 8;
            return getPrizesCount(item.ruleId);
          case 8:
            prizesCount = _context2.sent;
          case 9:
            if (!(prizesCount < 4 || prizesCount > 8)) {
              _context2.next = 12;
              break;
            }
            _message["default"].error((0, _locale.t)('setter.rulePrizeLengthError'));
            return _context2.abrupt("return");
          case 12:
            setSelectRule(function (rule) {
              return {
                dataType: (rule === null || rule === void 0 ? void 0 : rule.dataType) || _RuleSetterService.DataType.dataSource,
                value: item.value,
                isEven: prizesCount % 2 === 0
              };
            });
            _context2.next = 18;
            break;
          case 15:
            _context2.prev = 15;
            _context2.t0 = _context2["catch"](0);
            console.error(_context2.t0);
          case 18:
          case "end":
            return _context2.stop();
        }
      }, _callee2, null, [[0, 15]]);
    }));
    return function onChangeRule(_x2) {
      return _ref2.apply(this, arguments);
    };
  }();
  var onChangeDataType = function onChangeDataType(v) {
    setSelectRule({
      value: '',
      dataType: v
    });
  };

  //初始化选择 初始获取数据源及自定义变量
  (0, _react.useEffect)(function () {
    var webGid = (0, _utils.getLocationParamsByName)('webGid') || '';
    ruleSetterService.current = new _RuleSetterService.RuleSetterService(webGid);
    var _ruleSetterService$cu = ruleSetterService.current.getOptionsByDataSourceList(),
      dataSourceList = _ruleSetterService$cu.dataSourceList,
      customList = _ruleSetterService$cu.customList;
    setDataSourceSelectList(dataSourceList);
    setCustomList(customList);
    if (value) {
      if (dataType === _RuleSetterService.DataType.customState) {
        var count = 0;
        for (var i = 0; i < customList.length; i++) {
          if (customList[i].label === value) {
            count = customList[i].list.length;
            break;
          }
        }
        setSelectRule({
          value: value,
          isEven: count % 2 === 0,
          dataType: _RuleSetterService.DataType.customState
        });
      } else {
        var _dataSourceList$filte, _dataSourceList$filte2;
        var ruleId = dataSourceList === null || dataSourceList === void 0 ? void 0 : (_dataSourceList$filte = dataSourceList.filter(function (item) {
          return item.value === value;
        })) === null || _dataSourceList$filte === void 0 ? void 0 : (_dataSourceList$filte2 = _dataSourceList$filte[0]) === null || _dataSourceList$filte2 === void 0 ? void 0 : _dataSourceList$filte2.ruleId;
        (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
          var count;
          return _regenerator["default"].wrap(function _callee3$(_context3) {
            while (1) switch (_context3.prev = _context3.next) {
              case 0:
                if (ruleId) {
                  _context3.next = 2;
                  break;
                }
                return _context3.abrupt("return");
              case 2:
                _context3.next = 4;
                return getPrizesCount(ruleId);
              case 4:
                count = _context3.sent;
                setSelectRule({
                  value: value,
                  isEven: count % 2 === 0,
                  dataType: _RuleSetterService.DataType.dataSource
                });
              case 6:
              case "end":
                return _context3.stop();
            }
          }, _callee3);
        }))();
      }
    } else {
      setSelectRule({
        value: value,
        isEven: true,
        dataType: _RuleSetterService.DataType.dataSource
      });
    }
  }, []);
  (0, _react.useEffect)(function () {
    var obj = {
      dataSourceId: selectRule === null || selectRule === void 0 ? void 0 : selectRule.value,
      isEven: selectRule === null || selectRule === void 0 ? void 0 : selectRule.isEven,
      dataType: (selectRule === null || selectRule === void 0 ? void 0 : selectRule.dataType) || _RuleSetterService.DataType.dataSource
    };
    onChange(obj);
  }, [selectRule]);
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)('setter.dataType')
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)('setter.selectPlaceHolder'),
    dataSource: [{
      label: (0, _locale.t)('rotation-wheel.dataSource'),
      value: _RuleSetterService.DataType.dataSource
    }, {
      label: (0, _locale.t)('rotation-wheel.customState'),
      value: _RuleSetterService.DataType.customState
    }],
    value: selectRule === null || selectRule === void 0 ? void 0 : selectRule.dataType,
    name: "dataType",
    onChange: function onChange(v) {
      onChangeDataType(v);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)('setter.originTitle')
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)('setter.selectPlaceHolder'),
    dataSource: (selectRule === null || selectRule === void 0 ? void 0 : selectRule.dataType) === _RuleSetterService.DataType.customState ? customList : dataSourceSelectList,
    value: selectRule === null || selectRule === void 0 ? void 0 : selectRule.value,
    name: "ruleId",
    onChange: function onChange(v, type, item) {
      onChangeRule(item);
    }
  })));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(CRuleSelectSetter, this.props);
  };
  return _default;
}(_react.Component);