"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getSearchProps = void 0;
var _locale = require("@mirai/locale");
var _getOccupationTotals = require("../components/helpers/getOccupationTotals");
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 toIsoDate = value => value ? (0, _locale.dateFormat)(value, {
  delimiter: '-',
  format: 'YYYY-MM-DD'
}) : undefined;
const getSource = function () {
  var _ref;
  let urlParams = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  const raw = (_ref = urlParams.utm_source || urlParams.source || urlParams.utm_medium) === null || _ref === void 0 ? void 0 : _ref.toLowerCase();
  const map = {
    google_ha: 'google_ha',
    googleha: 'google_ha',
    'google-hotel-ads': 'google_ha',
    hotelads: 'google_ha',
    tripadvisor: 'tripadvisor',
    trivago: 'trivago',
    skyscanner: 'skyscanner'
  };
  if (raw && map[raw]) return map[raw];
  if (urlParams.gclid || ['cpc', 'ppc', 'sem', 'paid'].includes(raw)) return 'ads';
  if (raw) return raw;
  return 'direct';
};
const getOccupationProps = function () {
  let {
    occupation = [],
    infantAge
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  if (!(occupation !== null && occupation !== void 0 && occupation.length)) return {};
  const {
    adults = 0,
    babies = 0,
    children = 0
  } = (0, _getOccupationTotals.getOccupationTotals)({
    infantAge,
    occupation
  });
  const roomCount = occupation.length;
  const totalPax = adults + babies + children;
  return {
    is_multiroom: roomCount > 1,
    room_count: roomCount,
    total_pax: totalPax,
    adults,
    children,
    babies
  };
};
const getSearchProps = function () {
  var _urlParams$utm_source;
  let {
    store = {},
    dataSource = {}
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  const {
    finder = {},
    currency,
    language,
    locale,
    urlParams = {}
  } = store;
  const {
    calendar = [],
    occupation = [],
    promocode
  } = finder;
  const [checkin, checkout] = calendar;
  const props = _objectSpread({
    source: getSource(urlParams),
    language,
    locale,
    currency,
    promo_code: !!promocode,
    utm_source: (_urlParams$utm_source = urlParams.utm_source) === null || _urlParams$utm_source === void 0 ? void 0 : _urlParams$utm_source.toLowerCase(),
    checkin: toIsoDate(checkin),
    checkout: toIsoDate(checkout),
    lead_time_days: (0, _locale.dateDiff)(checkin, checkout).days
  }, getOccupationProps({
    occupation,
    infantAge: dataSource.infantAge
  }));
  return Object.fromEntries(Object.entries(props).filter(_ref2 => {
    let [, value] = _ref2;
    return value !== undefined && value !== '';
  }));
};
exports.getSearchProps = getSearchProps;
//# sourceMappingURL=getSearchProps.js.map