"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); }
const getAvailableFilters = function () {
  let items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  let {
    priceFactor = 1
  } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  const filters = {
    amenities: [],
    bedTypes: [],
    boards: [],
    freeCancellation: undefined,
    prices: [],
    sizes: [],
    sizeUnit: undefined
  };

  // ! TODO: Should order by price asc?
  items.filter(function () {
    let {
      availability = 0
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    return availability > 0;
  }).forEach(function () {
    let {
      amenities = [],
      bedTypes = [],
      boards = [],
      cancellations = [],
      size: rawSize = ''
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    amenities.forEach(function () {
      let {
        highlight,
        id,
        text
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      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 {
        id,
        name: text,
        type,
        price
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      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 [firstFreeCancellation] = !filters.freeCancellation ? cancellations.filter(_ref => {
      let {
        id
      } = _ref;
      return id.toLowerCase().includes(_Rates.FREE_CANCELLATION);
    }) : [];
    if (firstFreeCancellation) {
      const {
        id,
        name: text,
        title: caption
      } = firstFreeCancellation;
      filters.freeCancellation = {
        id,
        text,
        caption
      };
    }
    const [size, unit] = rawSize.split(' ');
    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(_ref2 => {
    let {
      highlight
    } = _ref2;
    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