/*!
 * Crafted with ❤ by Salla
 */
'use strict';

var index = require('./index-vstkdqRz.js');

const sallaMultipleBundleProductCss = ":host{display:block}";

const SallaMultipleBundleProduct = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.parsedSections = [];
    }
    parseProducts(newValue) {
        let sections = [];
        if (typeof newValue === 'string') {
            try {
                const parsed = JSON.parse(newValue);
                sections = this.extractSections(parsed);
            }
            catch (e) {
                console.error('Invalid JSON passed to bundleSections prop:', newValue);
                sections = [];
            }
        }
        else if (newValue) {
            sections = this.extractSections(newValue);
        }
        this.parsedSections = sections;
    }
    extractSections(data) {
        // Handle new nested structure: data.bundle.sections
        if (data.bundle && data.bundle.sections && Array.isArray(data.bundle.sections)) {
            return data.bundle.sections;
        }
        // Handle old flat structure: data is directly an array of sections
        if (Array.isArray(data)) {
            return data;
        }
        // Handle case where data.sections exists at root level
        if (data.sections && Array.isArray(data.sections)) {
            return data.sections;
        }
        console.warn('No valid sections found in data:', data);
        return [];
    }
    isCartPage() {
        return salla.url.is_page('cart');
    }
    renderCartPage() {
        return index.h("salla-multiple-bundle-product-cart", { sections: this.parsedSections });
    }
    renderDetailsPage() {
        return index.h("salla-multiple-bundle-product-details", { sections: this.parsedSections });
    }
    componentWillLoad() {
        this.parseProducts(this.bundleSections);
    }
    render() {
        return (index.h(index.Host, { key: '0c083bcf6f932c3db65e7c9c1871843594bc8604', class: "s-multiple-bundle-product-wrapper" }, this.isCartPage() ? this.renderCartPage() : this.renderDetailsPage()));
    }
    static get watchers() { return {
        "bundleSections": ["parseProducts"]
    }; }
};
SallaMultipleBundleProduct.style = sallaMultipleBundleProductCss;

exports.salla_multiple_bundle_product = SallaMultipleBundleProduct;
