"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getClub = void 0;
var _Core = require("../Core.constants");
const getClub = function () {
  let _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
    _ref$config = _ref.config,
    config = _ref$config === void 0 ? {} : _ref$config,
    type = _ref.type,
    _ref$urlParams = _ref.urlParams,
    urlParams = _ref$urlParams === void 0 ? {} : _ref$urlParams;
  let _config$accomodation = config.accomodation,
    _config$accomodation2 = _config$accomodation === void 0 ? {} : _config$accomodation,
    _config$accomodation3 = _config$accomodation2.club,
    _config$accomodation4 = _config$accomodation3 === void 0 ? {} : _config$accomodation3,
    authentication = _config$accomodation4.authentication,
    _config$accomodation5 = _config$accomodation4.discount,
    discountEnabled = _config$accomodation5 === void 0 ? false : _config$accomodation5,
    id = _config$accomodation4.id,
    name = _config$accomodation4.name,
    _config$accomodation6 = _config$accomodation4.rates,
    rates = _config$accomodation6 === void 0 ? false : _config$accomodation6,
    _config$accomodation7 = _config$accomodation4.url,
    url = _config$accomodation7 === void 0 ? {} : _config$accomodation7,
    _config$accomodation8 = _config$accomodation2.variants,
    variants = _config$accomodation8 === void 0 ? [] : _config$accomodation8;
  let active = !!id;
  if (!active) return;
  const applyClubDiscount = urlParams.applyClubDiscount,
    _urlParams$idtokenpro = urlParams.idtokenprovider,
    idHotel = _urlParams$idtokenpro === void 0 ? '' : _urlParams$idtokenpro;
  let discount = applyClubDiscount ? applyClubDiscount === 'true' : undefined;
  const nextVariants = variants.map(function () {
    let _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
      club = _ref2.club,
      id = _ref2.id;
    if (club && Object.keys(club).length === 0) return {
      active: false,
      id
    };
    const _ref3 = club || {},
      variantDiscount = _ref3.discount,
      variantRates = _ref3.rates;
    return {
      active: true,
      discount: variantDiscount !== undefined ? variantRates !== undefined ? variantRates ? variantDiscount : undefined : rates ? variantDiscount : undefined : (variantRates !== null && variantRates !== void 0 ? variantRates : rates) ? discountEnabled : undefined,
      discountEnabled: variantRates !== null && variantRates !== void 0 ? variantRates : rates,
      id
    };
  });
  if (type === _Core.TYPE.CHAIN) {
    const variant = nextVariants.find(function () {
      let _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref4$id = _ref4.id,
        id = _ref4$id === void 0 ? '' : _ref4$id;
      return id.toString() === idHotel.toString();
    });
    if (variant) {
      const variantActive = variant.active,
        variantDiscount = variant.discount,
        variantDiscountEnabled = variant.discountEnabled;
      active = variantActive;
      discount = discount !== null && discount !== void 0 ? discount : variantDiscount;
      discountEnabled = variantDiscountEnabled;
    }
  }
  discount = discount !== null && discount !== void 0 ? discount : discountEnabled;
  return {
    active,
    authentication,
    discount: active && rates ? discount : undefined,
    id,
    name,
    url,
    variants: nextVariants
  };
};
exports.getClub = getClub;
//# sourceMappingURL=getClub.js.map