/*!
 * Crafted with ❤ by Salla
 */
import { r as registerInstance, c as createEvent, h, H as Host } from './index-Cb4NFvMH.js';
import { c as closeModals, b as brand_path, f as formattedPointsJsx } from './functions-DqpTrGHa.js';
import { S as SIGift } from './gift-CXRKXFgT.js';
import { S as SITag } from './tag-cgSL_BIo.js';
import { S as SICheck } from './check-BZp0rKEO.js';
import { S as SICancel } from './cancel-8Tsmrwno.js';
import { T as TrackedPromise } from './tracked-promise-Dl5Y2VO1.js';

const sallaLoyaltyProgramCss$3 = "";

const SallaRewardAction = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.actionClick = createEvent(this, "actionClick");
        this.isShopNow = (prize) => {
            if (!(salla.config.get('store.features') || []).includes('loyalty-system-v2')) {
                return false;
            }
            return prize.key?.toLowerCase() === 'free_product';
        };
        this.handleClick = (e) => {
            if (salla.config.isGuest()) {
                closeModals(() => salla.event.dispatch('login::open'));
                return;
            }
            this.actionClick.emit(e);
        };
    }
    render() {
        return (h("div", { key: '0d02cecaae79cf166555514890fbf6192e1cd438', class: "s-loyalty-program-rewards-modal-action" }, this.isShopNow(this.prize) ? (h("a", { href: this.prize.prize_url || salla.url.get(''), class: {
                's-loyalty-program-rewards-action-base': true,
                's-loyalty-program-rewards-action-on': true,
            } }, salla.lang.get('pages.loyalty_program.shop_now'))) : (h("button", { onClick: this.handleClick, class: {
                's-loyalty-program-rewards-action-base': true,
                's-loyalty-program-rewards-action-on': true,
            } }, salla.lang.get('pages.loyalty_program.redeem_reward')))));
    }
};
SallaRewardAction.style = sallaLoyaltyProgramCss$3;

const sallaLoyaltyProgramCss$2 = "";

const SallaRewardCard = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.areaClick = createEvent(this, "areaClick");
        this.actionClick = createEvent(this, "actionClick");
    }
    render() {
        return (h(Host, { key: '0bd68f7cab478d78db8b083ea11c2090d02aab59', class: "s-loyalty-program-reward-host" }, h("div", { key: 'ef4e857bc55a12e50434c1dda2160674ead3b4e6', class: "s-loyalty-program-reward-container" }, h("div", { key: 'a6a7ccdf50ee296b48c05a01f241192c0f77f30c', onClick: (e) => this.areaClick.emit(e), role: "button" }, h("div", { key: 'e4f55cda0d9f36af95ce101c987819d30b9ad143', class: "s-loyalty-program-reward-img" }, h("img", { key: '450fe8914dcf809005d4707074cb305a59990e81', alt: "", src: this.prize.image || salla.url.cdn('images/s-empty.png'), class: "s-loyalty-program-rewards-img" })), h("div", { key: '7828210caf948e057055c53dc64af45426d7977d', class: "s-loyalty-program-reward-content" }, h("div", { key: '2f39695bd03884f6988b44de68964cdfdb75e0bb', class: "s-loyalty-program-reward-points" }, h("salla-badge", { key: 'e9e1dda91aa414d58caaff52a4bb667a1fc1b96f', size: "xs" }, h("span", { key: '7baa15af74cde5a6e744667566377417233d0848', innerHTML: SIGift }), h("span", { key: '6bd7f77ec736a6f0fd9de0c69907cc05e87829b0', class: "s-loyalty-program-rewards-points" }, this.prize.cost_points, " ", salla.lang.get('pages.loyalty_program.points')))), h("div", { key: '269da0571a6533ca6621792a3891f4b9d132d337', class: "s-loyalty-program-reward-title" }, h("span", { key: '06c40cc994031777565d43c5259eefb3311dea98' }, this.prize.group_title)), h("div", { key: 'e81936c993e83f96f4143278ec82391135778383', class: "s-loyalty-program-reward-name" }, h("div", { key: '752e5e6fbc5de60f7047913d6b8bf79e597fd407', class: "s-loyalty-program-rewards-name", id: `prize-${this.prize.id}` }, this.prize.name, this.prize.description ? `: ${this.prize.description}` : '')))), h("div", { key: 'a83282f66ab42ee0fa055cf9bf7802af1615e8f0', class: "p-4" }, h("slot", { key: '7186f03318c907b3e8ec15e30322d8bf787d74f8', name: "action" }, h("button", { key: '7b1a13734d2c230dd32d744a42e82fdb1c148660', onClick: (e) => this.actionClick.emit(e.detail), class: "s-loyalty-program-rewards-action-base s-loyalty-program-rewards-action-on" }, salla.lang.get('pages.loyalty_program.show_reward')))))));
    }
};
SallaRewardCard.style = sallaLoyaltyProgramCss$2;

const sallaLoyaltyProgramCss$1 = "";

var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const c = (...args) => args.map(i => 's-loyalty-program-' + i).join(' ');
const SallaRewardDetails = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.actionClick = createEvent(this, "actionClick");
        this.refreshKey = Date.now();
    }
    handlePrizeChange() {
        this.refreshKey = Date.now();
    }
    componentWillLoad() {
        if (this.category.status === 'idle') {
            this.category();
        }
        if (this.brand.status === 'idle') {
            this.brand();
        }
    }
    getTermsAndConditions() {
        const minimumAmount = Number((this.prize.order_minimum_amount || '0').split('.')[0]) || 0;
        const maximumAmount = Number((this.prize.coupon_maximum_amount || '0').split('.')[0]) || 0;
        const termsAndConditions = [];
        if (minimumAmount) {
            termsAndConditions.push([
                salla.lang.get('pages.loyalty_program.order_minimum_amount'),
                `${minimumAmount} ${salla.lang.get(`currency.${(salla.config.get('user.currency_code') || '').toLowerCase()}`)}`,
            ]);
        }
        if (maximumAmount) {
            termsAndConditions.push([
                salla.lang.get('pages.loyalty_program.coupon_maximum_amount'),
                `${maximumAmount} ${salla.lang.get(`currency.${(salla.config.get('user.currency_code') || '').toLowerCase()}`)}`,
            ]);
        }
        return termsAndConditions;
    }
    findItems(ids) {
        if (!ids?.length)
            return [];
        const brands = this.brand.data;
        const categories = this.category.data;
        // id_ is the numeric ID from the API (e.g. 740744759); id is a string hash (e.g. "zEVZGx"). included_category_ids/included_brand_ids are number[], so id_ is correct here.
        return brands.concat(categories).filter(i => ids.includes(i.id_));
    }
    getIncludedItems(items, ids) {
        if (items?.length) {
            return items;
        }
        if (ids?.length) {
            return this.findItems(ids);
        }
        return [];
    }
    getProductIds(ids) {
        return ids || [];
    }
    getItemsMarkers(key) {
        if (key === 'both') {
            return this.getItemsMarkers('included') + this.getItemsMarkers('excluded');
        }
        return [
            this.prize[`${key}_product_ids`]?.length || 0,
            this.prize[`${key}_brand_ids`]?.length || 0,
            this.prize[`${key}_category_ids`]?.length || 0,
        ].map(Boolean).map(Number).join('');
    }
    getItemsMarkersText() {
        // Check if prize type is free_shipping
        if (this.prize.key === 'FREE_SHIPPING') {
            return salla.lang.get('pages.loyalty_program.redeem_for_free_shipping');
        }
        switch (this.getItemsMarkers('included')) {
            case '001':
                return salla.lang.get('pages.loyalty_program.redeem_on_specific_products');
            case '010':
                return salla.lang.get('pages.loyalty_program.redeem_on_specific_brands');
            case '100':
                return salla.lang.get('pages.loyalty_program.redeem_on_specific_categories');
            case '000':
                return salla.lang.get('pages.loyalty_program.redeem_on_all_products');
            default:
                return salla.lang.get('pages.loyalty_program.redeem_on_specific_products');
        }
    }
    getCategoriesJsx(items, title) {
        return !items.length ? '' : h("div", { class: c('reward-details-categories-wrapper') }, h("div", { class: c('reward-details-categories') }, h("h2", { class: c('reward-details-categories-heading') }, title), h("ul", { class: c('reward-details-categories-list') }, items.map((category) => (h("li", null, h("a", { href: category.url || category.lingual_urls?.[0], class: c('reward-details-categories-item') }, h("span", { innerHTML: SITag }), h("span", { class: c('reward-details-categories-item-content') }, category.name))))))));
    }
    getBrandsJsx(items, title) {
        return !items.length ? '' : h("div", { class: c('reward-details-items-brands', 'slider-arrows-edges') }, h("div", null, h("h2", null, title), h("salla-slider", { id: `brands-${items.map(({ id }) => id).join('-')}`, "slider-config": '{"loop":false,"slidesPerView":"auto","spaceBetween":10}' }, h("div", { slot: "items" }, items.map((brand) => (h("a", { href: salla.url.get(brand_path(brand)), class: c('reward-brand-item') }, h("img", { src: brand.logo || salla.url.cdn('images/s-empty.png'), alt: "" }))))))));
    }
    getProductsJsx(items, title) {
        return !items?.length ? '' : h("div", { class: "s-loyalty-program-reward-details-items-products s-loyalty-program-slider-arrows-edges" }, h("h2", null, title), h("salla-products-slider", { source: "selected", sourceValue: JSON.stringify(items), "slider-config": '{"loop":false,"slidesPerView":"auto","freeMode":true,"spaceBetween":0,"breakpoints":{"768":{"slidesPerView":3,"freeMode":false,"speed":300}}}' }));
    }
    render() {
        const termsAndConditions = this.getTermsAndConditions();
        return (h("div", { key: '17b29a9098873643557786c9d9515b6245e2d773', class: "s-loyalty-program-reward-modal" }, h("div", { key: 'bd5411918a07a8e2dc1eb8ef7b1e06e82403d280', class: "s-loyalty-program-points-modal-header" }, h("h1", { key: '9e6d884e3e70c0ff3dbb005f61d48c1d1fa2ee65', class: "s-loyalty-program-points-modal-info" }, !salla.url.is_page('cart') ? ''
            : h("button", { class: "s-loyalty-program-back-btn", onClick: () => closeModals(() => salla.event.dispatch('modal::open', 'loyalty-rewards-modal')) }, h("span", { class: {
                    'sicon-arrow-right': document.documentElement.getAttribute('dir') === 'rtl',
                    'sicon-arrow-left': document.documentElement.getAttribute('dir') === 'ltr',
                } })), this.prize.name)), h("div", { key: '09d716d984701a251a3a759acbf112883fd0cf8f', class: "s-loyalty-program-modal-scrollable-content" }, h("div", { key: 'f7e2c8d017ba4dab380d08e0c7a5f1e49416b1f4', class: "s-loyalty-program-reward-details-header" }, h("div", { key: '369bb4e9a1b8d8dde62de1236b4d836fd3d6841f', class: "s-loyalty-program-aspect-container" }, h("img", { key: '3ed20feecdcd1c7433ec465364ae62639824ee6d', src: this.prize.image, alt: this.prize.name })), h("salla-badge", { key: '2bba4702e75d05622b719fa796efccda0520ce69', size: "sm", bg: "light" }, h("span", { key: '837afff2bdf9a390e8ca373f31dddbd84d7c6a91', innerHTML: SIGift }), h("span", { key: 'c3631a877062d69947928b1beee2ae4932910eec', class: "s-loyalty-program-points-badge-points" }, formattedPointsJsx(this.prize.cost_points)))), !termsAndConditions.length ? '' : h("div", { class: "s-loyalty-program-terms-and-conditions" }, h("h1", { class: "s-loyalty-program-terms-and-conditions-heading" }, salla.lang.get('pages.loyalty_program.terms_and_conditions')), h("ul", { class: "s-loyalty-program-terms-and-conditions-list" }, termsAndConditions.map(([title, points]) => (h("li", { class: "s-loyalty-program-terms-and-conditions-item" }, h("span", { class: "s-loyalty-program-terms-and-conditions-content" }, title), h("span", { class: "s-loyalty-program-terms-and-conditions-content" }, points)))))), h("div", { key: '872bfd776acf67910a5cdeafdb48e2c7afd5d71c', class: "s-loyalty-program-reward-info-message" }, h("h1", { key: '253e6f58946e911ba2b74ef18ebcee0eae0bb514', class: "s-loyalty-program-reward-info-message-heading" }, salla.lang.get('pages.loyalty_program.how_to_benefit_from_this_reward')), h("p", { key: 'ce4b30fab9f1fae89fd956ae3eb20e5750fcea09', class: "s-loyalty-program-reward-info-message-content" }, this.getItemsMarkersText())), (this.prize.key === 'FREE_SHIPPING' || this.getItemsMarkers('both') === '000000') ? '' : h("div", { class: "s-loyalty-program-reward-details" }, h("salla-tabs", { key: this.refreshKey }, this.getItemsMarkers('included') === '000' ? '' : [
            h("salla-tab-header", { slot: "header", name: "included", style: {
                    display: this.getItemsMarkers('excluded') === '000' ? 'none' : 'block',
                } }, h("span", { class: "s-loyalty-program-reward-details-tab-icon", innerHTML: SICheck }), h("span", null, salla.lang.get(`pages.loyalty_program.included`))),
            h("salla-tab-content", { slot: "content", name: "included" }, h("div", { class: "s-loyalty-program-reward-details-items-wrapper" }, this.getCategoriesJsx(this.getIncludedItems(this.prize.categories, this.prize.included_category_ids), salla.lang.get('pages.loyalty_program.included_categories')), this.getBrandsJsx(this.getIncludedItems(this.prize.brands, this.prize.included_brand_ids), salla.lang.get('pages.loyalty_program.included_brands')), this.getProductsJsx(this.getProductIds(this.prize.included_product_ids), salla.lang.get('pages.loyalty_program.included_products')))),
        ], this.getItemsMarkers('excluded') === '000' ? '' : [
            h("salla-tab-header", { slot: "header", name: "excluded", style: {
                    display: this.getItemsMarkers('included') === '000' ? 'none' : 'block',
                } }, h("span", { class: "s-loyalty-program-reward-details-tab-icon", innerHTML: SICancel }), h("span", null, salla.lang.get(`pages.loyalty_program.excluded`))),
            h("salla-tab-content", { slot: "content", name: "excluded" }, h("div", { class: "s-loyalty-program-reward-details-items-wrapper" }, this.getCategoriesJsx(this.getIncludedItems(this.prize.excludedCategories, this.prize.excluded_category_ids), salla.lang.get('pages.loyalty_program.excluded_categories')), this.getBrandsJsx(this.getIncludedItems(this.prize.excludedBrands, this.prize.excluded_brand_ids), salla.lang.get('pages.loyalty_program.excluded_brands')), this.getProductsJsx(this.getProductIds(this.prize.excluded_product_ids), salla.lang.get('pages.loyalty_program.excluded_products')))),
        ]))), h("salla-reward-action", { key: '4055a99cf9dd6a30448848544d302e929afb5f06', onActionClick: e => this.actionClick.emit(e.detail), prize: this.prize })));
    }
    static get watchers() { return {
        "prize": ["handlePrizeChange"]
    }; }
};
__decorate$1([
    TrackedPromise(async () => {
        return (await salla.product.api.categories()).data;
    }, { initialData: [] })
], SallaRewardDetails.prototype, "category", void 0);
__decorate$1([
    TrackedPromise(async () => {
        return (await salla.api.request('/brands')).data;
    }, { initialData: [] })
], SallaRewardDetails.prototype, "brand", void 0);
SallaRewardDetails.style = sallaLoyaltyProgramCss$1;

const sallaLoyaltyProgramCss = "";

var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
        r = Reflect.decorate(decorators, target, key, desc);
    else
        for (var i = decorators.length - 1; i >= 0; i--)
            if (d = decorators[i])
                r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
const SallaRewardExchange = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.productId = null;
    }
    reset(timeout = 0) {
        setTimeout(() => {
            closeModals();
        }, timeout);
    }
    async handleConfirm() {
        try {
            await this.exchange(this.prize.id, null, this.productId);
            this.reset(300);
        }
        catch (error) {
            salla.logger.error(error);
            // Do NOT close the modal; let the error state render so the user sees feedback.
        }
    }
    handleCancel() {
        this.reset(0);
    }
    render() {
        return (h(Host, { key: 'b5b9a966f28520c30917887621e2358fdb9530be' }, h("div", { key: '4e87e3d0530a7fca6db1bf09fd1b059b85e56f10', class: "s-loyalty-program-exchange-heading" }, h("div", { key: '73b9aa3106336e8cdc81ce78371b3efc47b5c569', class: "s-loyalty-program-circle-base s-loyalty-program-circle-size-xl s-loyalty-program-circle-variant-fill" }, h("span", { key: '0e6c488afa39784a40bfed2f1d180fc48664a81a', innerHTML: SIGift })), h("h2", { key: '7740e27318379264905ff695e0c0cf13b54800d0' }, salla.lang.get('pages.loyalty_program.redeem_reward')), h("p", { key: '81a7ba841e3e26c7c9fc1054745ad163cc347db7' }, salla.lang.get('pages.loyalty_program.redeem_confirmation'), " ", h("span", { key: '0f5d68dbe2de661c2aad02d237d3098349e94120' }, "(", this.prize.cost_points, " ", salla.lang.get('pages.loyalty_program.point'), ")"), " ", salla.lang.get('pages.loyalty_program.for'), " ", h("span", { key: '1daa10a1843938166c6834d0369edfe7e06dd54e' }, "(", this.prize.name, ")"))), h("div", { key: '42384b2df2cbb3775ccc6f7a017d9ef9f770baf5', class: "s-loyalty-program-exchange-btns" }, h("button", { key: 'b90b95adae80a9bcb47c0d039ea8bd9399ee5bc5', class: "s-loyalty-program-rewards-action-base s-loyalty-program-rewards-action-confirm", onClick: () => this.handleConfirm() }, h("span", { key: 'c108973d9729265fb35a0d0fa781d525388a4c12', style: {
                opacity: this.exchange.status !== 'pending' ? '1' : '0',
            } }, salla.lang.get('common.elements.confirm')), h("span", { key: '302a31b9095d153b4662ada13d45cdb6b15664ad', class: "s-loyalty-program-exchange-loading", style: {
                opacity: this.exchange.status === 'pending' ? '1' : '0',
            } }, h("salla-loading", { key: '3e305da32d469f6ec874c128a1fd68356280dc69', color: "primary", size: 24 }))), h("button", { key: '466f8cfea3e370bce9466e9571f4567fb5a7966d', class: "s-loyalty-program-rewards-action-base s-loyalty-program-rewards-action-cancel", onClick: () => this.handleCancel() }, salla.lang.get('common.elements.cancel')))));
    }
};
__decorate([
    TrackedPromise(async (...args) => await salla.loyalty.exchange(...args))
], SallaRewardExchange.prototype, "exchange", void 0);
SallaRewardExchange.style = sallaLoyaltyProgramCss;

export { SallaRewardAction as salla_reward_action, SallaRewardCard as salla_reward_card, SallaRewardDetails as salla_reward_details, SallaRewardExchange as salla_reward_exchange };
