"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toOccupation = void 0;
var _shared__ = require("../components/__shared__");
var _helpers = require("../components/Finder/helpers");
const {
  GUEST,
  ADULT,
  CHILD
} = _shared__.TYPE_OCCUPATION;
const toOccupation = _ref => {
  let {
    chainSelectorValue,
    occupation,
    parties,
    places
  } = _ref;
  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 {
      adults = 0,
      children = []
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    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 ? void 0 : dataSource.forEach(function () {
      let {
        type,
        age: [, max] = []
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      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