"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getClub = void 0;
var _Core = require("../Core.constants");
const getClub = function () {
  var _discount2;
  let {
    config = {},
    type,
    urlParams = {}
  } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  let {
    accomodation: {
      club: {
        authentication,
        discount: discountEnabled = false,
        id,
        name,
        rates = false,
        url = {}
      } = {},
      variants = []
    } = {}
  } = config;
  let active = !!id;
  if (!active) return;
  const {
    applyClubDiscount,
    idtokenprovider: idHotel = ''
  } = urlParams;
  let discount = applyClubDiscount ? applyClubDiscount === 'true' : undefined;
  const nextVariants = variants.map(function () {
    let {
      club,
      id
    } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    if (club && Object.keys(club).length === 0) return {
      active: false,
      id
    };
    const {
      discount: variantDiscount,
      rates: variantRates
    } = club || {};
    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 {
        id = ''
      } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      return id.toString() === idHotel.toString();
    });
    if (variant) {
      var _discount;
      const {
        active: variantActive,
        discount: variantDiscount,
        discountEnabled: variantDiscountEnabled
      } = variant;
      active = variantActive;
      discount = (_discount = discount) !== null && _discount !== void 0 ? _discount : variantDiscount;
      discountEnabled = variantDiscountEnabled;
    }
  }
  discount = (_discount2 = discount) !== null && _discount2 !== void 0 ? _discount2 : discountEnabled;
  return {
    active,
    authentication,
    discount: active && rates ? discount : undefined,
    id,
    name,
    url,
    variants: nextVariants
  };
};
exports.getClub = getClub;
//# sourceMappingURL=getClub.js.map