"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getAvailableFilters = void 0;
var _Rates = require("../../../Rates.constants");
var _Filters = require("../Filters.constants");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 getAvailableFilters = function () {
  let items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  let _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
    _ref$priceFactor = _ref.priceFactor,
    priceFactor = _ref$priceFactor === void 0 ? 1 : _ref$priceFactor;
  const filters = {
    amenities: [],
    bedTypes: [],
    boards: [],
    freeCancellation: undefined,
    prices: [],
    sizes: [],
    sizeUnit: undefined
  };

  // ! TODO: Should order by price asc?
  items.filter(function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref2$availability = _ref2.availability,
      availability = _ref2$availability === void 0 ? 0 : _ref2$availability;
    return availability > 0;
  }).forEach(function () {
    let _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      _ref3$amenities = _ref3.amenities,
      amenities = _ref3$amenities === void 0 ? [] : _ref3$amenities,
      _ref3$bedTypes = _ref3.bedTypes,
      bedTypes = _ref3$bedTypes === void 0 ? [] : _ref3$bedTypes,
      _ref3$boards = _ref3.boards,
      boards = _ref3$boards === void 0 ? [] : _ref3$boards,
      _ref3$cancellations = _ref3.cancellations,
      cancellations = _ref3$cancellations === void 0 ? [] : _ref3$cancellations,
      _ref3$size = _ref3.size,
      rawSize = _ref3$size === void 0 ? '' : _ref3$size;
    amenities.forEach(function () {
      let _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        highlight = _ref4.highlight,
        id = _ref4.id,
        text = _ref4.text;
      if (!filters.amenities.find(function () {
        let item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        return item.id === id;
      })) filters.amenities.push({
        highlight,
        id,
        text
      });
    });
    bedTypes.forEach(function () {
      let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
      if (!filters.bedTypes.find(item => item === id)) filters.bedTypes.push(id);
    });
    boards.forEach(function () {
      let _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        id = _ref5.id,
        text = _ref5.name,
        type = _ref5.type,
        price = _ref5.price;
      if (!filters.boards.find(function () {
        let item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
        return item.type === type;
      })) filters.boards.push({
        id,
        text,
        type
      });
      filters.prices.push(price / priceFactor);
    });
    const _ref6 = !filters.freeCancellation ? cancellations.filter(_ref8 => {
        let id = _ref8.id;
        return id.toLowerCase().includes(_Rates.FREE_CANCELLATION);
      }) : [],
      _ref7 = _slicedToArray(_ref6, 1),
      firstFreeCancellation = _ref7[0];
    if (firstFreeCancellation) {
      const id = firstFreeCancellation.id,
        text = firstFreeCancellation.name,
        caption = firstFreeCancellation.title;
      filters.freeCancellation = {
        id,
        text,
        caption
      };
    }
    const _rawSize$split = rawSize.split(' '),
      _rawSize$split2 = _slicedToArray(_rawSize$split, 2),
      size = _rawSize$split2[0],
      unit = _rawSize$split2[1];
    if (size.length > 0 && !filters.sizes.find(function () {
      let item = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return item.size === parseInt(size);
    })) {
      if (unit && !filters.sizeUnit) filters.sizeUnit = unit;
      filters.sizes.push(parseInt(size));
    }
  });

  // Clean filters
  if (!filters.amenities.some(_ref9 => {
    let highlight = _ref9.highlight;
    return highlight;
  })) {
    filters.amenities.forEach(function () {
      let filter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      let index = arguments.length > 1 ? arguments[1] : undefined;
      filters.amenities[index] = _objectSpread(_objectSpread({}, filter), {}, {
        highlight: index < _Filters.MAX_AMENITIES
      });
    });
  }
  filters.bedTypes = filters.bedTypes.sort((a, b) => a - b);
  if (filters.boards.length < _Filters.MIN_BOARDS) filters.boards = [];else if (filters.boards.length > _Filters.MAX_BOARDS) filters.boards = filters.boards.slice(0, _Filters.MAX_BOARDS);
  return filters;
};
exports.getAvailableFilters = getAvailableFilters;
//# sourceMappingURL=getAvailableFilters.js.map