"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.consolidateValue = void 0;
var _Occupation = require("../Occupation.constants");
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
const consolidateValue = function () {
  let value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  let dataSource = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  let currentDataSource = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  const nextValue = [];
  let completed = true;
  value.every(function () {
    let room = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    let roomIndex = arguments.length > 1 ? arguments[1] : undefined;
    let totalAmount = 0;
    room.every(function () {
      var _nextValue$roomIndex;
      let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        amount = _ref.amount,
        _ref$ages = _ref.ages,
        ages = _ref$ages === void 0 ? [] : _ref$ages,
        type = _ref.type;
      totalAmount += amount;
      ages.every(age => {
        let slot = false;
        dataSource.forEach((_ref2, dataSourceIndex) => {
          let nextType = _ref2.type,
            _ref2$age = _ref2.age,
            _ref2$age2 = _ref2$age === void 0 ? [] : _ref2$age,
            _ref2$age3 = _slicedToArray(_ref2$age2, 2),
            _ref2$age3$ = _ref2$age3[0],
            min = _ref2$age3$ === void 0 ? 0 : _ref2$age3$,
            _ref2$age3$2 = _ref2$age3[1],
            max = _ref2$age3$2 === void 0 ? 99 : _ref2$age3$2,
            fulfill = _ref2.fulfill;
          if (slot || typeof type !== 'number') return undefined;
          let _ref3 = currentDataSource.find(_ref4 => {
              let currentAge = _ref4.age,
                currentType = _ref4.type;
              return currentType === nextType && (currentAge === null || currentAge === void 0 ? void 0 : currentAge.length) && currentAge[0] != min;
            }) || {},
            _ref3$age = _ref3.age,
            _ref3$age2 = _ref3$age === void 0 ? [] : _ref3$age,
            _ref3$age3 = _slicedToArray(_ref3$age2, 1),
            currentMin = _ref3$age3[0];
          const sameType = nextType === parseInt(type);
          if (currentMin === null) currentMin = undefined;
          if (nextType === _Occupation.TYPE.GUEST || age >= min && age <= max || fulfill && age === undefined && sameType || !fulfill && sameType && !currentMin || !fulfill && sameType && currentMin && min <= currentMin) slot = true;
          if (slot) {
            nextValue[roomIndex] = nextValue[roomIndex] || [];
            nextValue[roomIndex][dataSourceIndex] = nextValue[roomIndex][dataSourceIndex] || {
              ages: [],
              amount: 0,
              type: nextType
            };
            if (nextType === _Occupation.TYPE.GUEST) {
              nextValue[roomIndex][dataSourceIndex].amount = totalAmount;
            } else {
              nextValue[roomIndex][dataSourceIndex].amount += 1;
            }
            nextValue[roomIndex][dataSourceIndex].ages.push(age);
          }
        });
        if (!slot) completed = false;
        return completed;
      });
      const isCorrectAmount = !((_nextValue$roomIndex = nextValue[roomIndex]) !== null && _nextValue$roomIndex !== void 0 && _nextValue$roomIndex.some(_ref5 => {
        let nextAmount = _ref5.amount,
          nextType = _ref5.type;
        const _ref6 = dataSource.find(_ref7 => {
            let itemType = _ref7.type;
            return itemType === nextType;
          }) || {},
          max = _ref6.max,
          min = _ref6.min;
        return nextAmount > max || nextAmount < min;
      }));
      if (!isCorrectAmount) completed = false;
      return completed;
    });
    return completed;
  });
  return completed ? nextValue : undefined;
};
exports.consolidateValue = consolidateValue;
//# sourceMappingURL=consolidateValue.js.map