"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toOccupation = void 0;
var _shared__ = require("../components/__shared__");
var _helpers = require("../components/Finder/helpers");
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 GUEST = _shared__.TYPE_OCCUPATION.GUEST,
  ADULT = _shared__.TYPE_OCCUPATION.ADULT,
  CHILD = _shared__.TYPE_OCCUPATION.CHILD;
const toOccupation = _ref => {
  let chainSelectorValue = _ref.chainSelectorValue,
    occupation = _ref.occupation,
    parties = _ref.parties,
    places = _ref.places;
  const value = [];
  let decodedParties = [];
  try {
    decodedParties = JSON.parse(window.atob(parties)) || [];
    // eslint-disable-next-line no-empty
  } catch (_unused) {}
  const dataSource = (0, _helpers.getOccupationDataSource)({
    occupation: occupation.dataSource,
    place: Number(chainSelectorValue),
    places,
    variants: occupation.variants
  });
  decodedParties.forEach(function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref2$adults = _ref2.adults,
      adults = _ref2$adults === void 0 ? 0 : _ref2$adults,
      _ref2$children = _ref2.children,
      children = _ref2$children === void 0 ? [] : _ref2$children;
    let roomIndex = arguments.length > 1 ? arguments[1] : undefined;
    children = children.map(item => item == null ? undefined : item);
    const rooms = [];
    const checked = [];
    dataSource === null || dataSource === void 0 || dataSource.forEach(function () {
      let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        type = _ref3.type,
        _ref3$age = _ref3.age,
        _ref3$age2 = _ref3$age === void 0 ? [] : _ref3$age,
        _ref3$age3 = _slicedToArray(_ref3$age2, 2),
        max = _ref3$age3[1];
      let index = arguments.length > 1 ? arguments[1] : undefined;
      if (adults > 0 && [ADULT, GUEST].includes(type)) {
        rooms[index] = {
          ages: Array(adults).fill(max),
          amount: adults,
          type
        };
      } else if (type === CHILD) {
        children.filter((_, index) => !checked.includes(index)).forEach((age, childIndex) => {
          const room = rooms[index] || {
            ages: [],
            amount: 0,
            type
          };
          room.amount += 1;
          room.ages.push(age);
          rooms[index] = room;
          checked.push(childIndex);
        });
      }
    });
    value[roomIndex] = rooms.length ? rooms : undefined;
    if (value[roomIndex] && children.length > 0 && dataSource[0].type === GUEST) {
      value[roomIndex][0].amount = value[roomIndex][0].amount + children.length;
      children.forEach(() => value[roomIndex][0].ages.push(undefined));
    }
  });
  return value.filter(item => !!item).length ? value : undefined;
};
exports.toOccupation = toOccupation;
//# sourceMappingURL=toOccupation.js.map