/*!
 * Crafted with ❤ by Salla
 */
import { proxyCustomElement, HTMLElement, createEvent, h } from '@stencil/core/internal/client';
import { b as brand_path, c as closeModals, f as formattedPointsJsx } from './functions.js';
import { l as loyaltyProgramIcon } from './gift.js';
import { T as Tag } from './tag.js';
import { I as IconVerified } from './check.js';
import { C as Cancel } from './cancel.js';
import { T as TrackedPromise } from './tracked-promise.js';
import { d as defineCustomElement$h } from './salla-add-product-button2.js';
import { d as defineCustomElement$g } from './salla-badge2.js';
import { d as defineCustomElement$f } from './salla-button2.js';
import { d as defineCustomElement$e } from './salla-count-down2.js';
import { d as defineCustomElement$d } from './salla-loading2.js';
import { d as defineCustomElement$c } from './salla-modal2.js';
import { d as defineCustomElement$b } from './salla-product-availability2.js';
import { d as defineCustomElement$a } from './salla-product-card2.js';
import { d as defineCustomElement$9 } from './salla-products-slider2.js';
import { d as defineCustomElement$8 } from './salla-progress-bar2.js';
import { d as defineCustomElement$7 } from './salla-quick-buy2.js';
import { d as defineCustomElement$6 } from './salla-reward-action2.js';
import { d as defineCustomElement$5 } from './salla-slider2.js';
import { d as defineCustomElement$4 } from './salla-tab-content2.js';
import { d as defineCustomElement$3 } from './salla-tab-header2.js';
import { d as defineCustomElement$2 } from './salla-tabs2.js';
import { d as defineCustomElement$1 } from './salla-tel-input2.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 c = (...args) => args.map(i => 's-loyalty-program-' + i).join(' ');
const SallaRewardDetails = /*@__PURE__*/ proxyCustomElement(class SallaRewardDetails extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.actionClick = createEvent(this, "actionClick", 7);
        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: Tag }), 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: loyaltyProgramIcon }), 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: IconVerified }), 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: Cancel }), 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"]
    }; }
    static get style() { return sallaLoyaltyProgramCss; }
}, [0, "salla-reward-details", {
        "prize": [16],
        "refreshKey": [32]
    }, undefined, {
        "prize": ["handlePrizeChange"]
    }]);
__decorate([
    TrackedPromise(async () => {
        return (await salla.product.api.categories()).data;
    }, { initialData: [] })
], SallaRewardDetails.prototype, "category", void 0);
__decorate([
    TrackedPromise(async () => {
        return (await salla.api.request('/brands')).data;
    }, { initialData: [] })
], SallaRewardDetails.prototype, "brand", void 0);
function defineCustomElement() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["salla-reward-details", "salla-add-product-button", "salla-badge", "salla-button", "salla-count-down", "salla-loading", "salla-modal", "salla-product-availability", "salla-product-card", "salla-products-slider", "salla-progress-bar", "salla-quick-buy", "salla-reward-action", "salla-slider", "salla-tab-content", "salla-tab-header", "salla-tabs", "salla-tel-input"];
    components.forEach(tagName => { switch (tagName) {
        case "salla-reward-details":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, SallaRewardDetails);
            }
            break;
        case "salla-add-product-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$h();
            }
            break;
        case "salla-badge":
            if (!customElements.get(tagName)) {
                defineCustomElement$g();
            }
            break;
        case "salla-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$f();
            }
            break;
        case "salla-count-down":
            if (!customElements.get(tagName)) {
                defineCustomElement$e();
            }
            break;
        case "salla-loading":
            if (!customElements.get(tagName)) {
                defineCustomElement$d();
            }
            break;
        case "salla-modal":
            if (!customElements.get(tagName)) {
                defineCustomElement$c();
            }
            break;
        case "salla-product-availability":
            if (!customElements.get(tagName)) {
                defineCustomElement$b();
            }
            break;
        case "salla-product-card":
            if (!customElements.get(tagName)) {
                defineCustomElement$a();
            }
            break;
        case "salla-products-slider":
            if (!customElements.get(tagName)) {
                defineCustomElement$9();
            }
            break;
        case "salla-progress-bar":
            if (!customElements.get(tagName)) {
                defineCustomElement$8();
            }
            break;
        case "salla-quick-buy":
            if (!customElements.get(tagName)) {
                defineCustomElement$7();
            }
            break;
        case "salla-reward-action":
            if (!customElements.get(tagName)) {
                defineCustomElement$6();
            }
            break;
        case "salla-slider":
            if (!customElements.get(tagName)) {
                defineCustomElement$5();
            }
            break;
        case "salla-tab-content":
            if (!customElements.get(tagName)) {
                defineCustomElement$4();
            }
            break;
        case "salla-tab-header":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "salla-tabs":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
        case "salla-tel-input":
            if (!customElements.get(tagName)) {
                defineCustomElement$1();
            }
            break;
    } });
}
defineCustomElement();

export { SallaRewardDetails as S, defineCustomElement as d };
