"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.consolidateValue = void 0;
var _Occupation = require("../Occupation.constants");
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 {
        amount,
        ages = [],
        type
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      totalAmount += amount;
      ages.every(age => {
        let slot = false;
        dataSource.forEach((_ref, dataSourceIndex) => {
          let {
            type: nextType,
            age: [min = 0, max = 99] = [],
            fulfill
          } = _ref;
          if (slot || typeof type !== 'number') return undefined;
          let {
            age: [currentMin] = []
          } = currentDataSource.find(_ref2 => {
            let {
              age: currentAge,
              type: currentType
            } = _ref2;
            return currentType === nextType && (currentAge === null || currentAge === void 0 ? void 0 : currentAge.length) && currentAge[0] != min;
          }) || {};
          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(_ref3 => {
        let {
          amount: nextAmount,
          type: nextType
        } = _ref3;
        const {
          max,
          min
        } = dataSource.find(_ref4 => {
          let {
            type: itemType
          } = _ref4;
          return itemType === nextType;
        }) || {};
        return nextAmount > max || nextAmount < min;
      }));
      if (!isCorrectAmount) completed = false;
      return completed;
    });
    return completed;
  });
  return completed ? nextValue : undefined;
};
exports.consolidateValue = consolidateValue;
//# sourceMappingURL=consolidateValue.js.map