/*!
 * Crafted with ❤ by Salla
 */
import { r as registerInstance, h, a as getElement } from './index-Cb4NFvMH.js';
import { s as setTranslations } from './translations-B-Rj6b9M.js';
import { S as SIGift } from './gift-CXRKXFgT.js';
import { T as TrackedPromise } from './tracked-promise-Dl5Y2VO1.js';

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 SallaLoyaltyBanner = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
    }
    /**
     * Get product ID from data attribute or fallback to page.id
     */
    get productId() {
        return this.host.dataset.productId || salla.config.get('page.id');
    }
    componentWillLoad() {
        return salla.onReady()
            .then(() => salla.lang.onLoaded())
            .then(() => setTranslations())
            .then(() => this.prize())
            .catch(error => salla.logger.error(error));
    }
    handleClick() {
        if (salla.config.isGuest()) {
            salla.event.dispatch('login::open');
            return;
        }
        this.exchangeModal.open();
    }
    render() {
        if (this.prize.status !== 'success' || !this.productId) {
            return null;
        }
        return (h("div", { class: "s-loyalty-program-banner-container" }, h("div", null, h("p", { class: "s-loyalty-program-banner-container-banner-title" }, salla.lang.get('pages.loyalty_program.want_to_redeem_free_product')), h("p", { class: "s-loyalty-program-banner-container-banner-subtitle" }, salla.lang.get('pages.loyalty_program.exchange_points_for_free_product', { points: this.prize.data.cost_points }))), h("button", { onClick: () => this.handleClick() }, h("span", { innerHTML: SIGift }), h("span", null, salla.lang.get('pages.loyalty_program.exchange_points'))), h("salla-modal", { ref: modal => this.exchangeModal = modal, width: "sm" }, !this.prize.data ? '' : h("salla-reward-exchange", { productId: Number(this.productId), prize: this.prize.data }))));
    }
    get host() { return getElement(this); }
};
__decorate([
    TrackedPromise(async function () {
        if (!(salla.config.get('store.features') || []).includes('loyalty-system')) {
            return Promise.reject('Loyalty system is not enabled for merchant');
        }
        const { loyalty_prize } = await salla.product.api.getDetails(this.productId);
        if (!loyalty_prize) {
            return Promise.reject('no loyalty prize for this product');
        }
        return loyalty_prize;
    })
], SallaLoyaltyBanner.prototype, "prize", void 0);
SallaLoyaltyBanner.style = sallaLoyaltyProgramCss;

export { SallaLoyaltyBanner as salla_loyalty_banner };
