import { r as registerInstance, h, d as Host } from './index-c0a4d8bd.js';
import { w as weights } from './data-9f2b8a3b.js';
import { a as convertFileFromStream } from './convert-ecca01b6.js';
import { s as state } from './pn-find-price-store-8a0a5cf0.js';
import './index-a0a6fb88.js';

const translations = {
    sv: {
        componentheading: 'Våra priser och villkor för inrikesförsändelser för dig som direktbetalande kund?',
        deliveryscopeheading: 'Endast leveranser inom Sverige',
        resultlistheading: 'Några bra alternativ för dig',
        weightlabel: 'Vikt',
        maxweightlabel: 'Maxvikt',
        maxmeasurementlabel: 'Maxvolym',
        senddirectcta: 'Köp online',
        stampshopcta: 'Köp frimärken',
        message: '*Vid köp hos valfritt ombud',
        servicePointHeader: 'Eller hitta närmaste serviceställe',
        servicePointLink: 'Sök serviceställe eller brevlåda',
    },
    en: {
        componentheading: 'Our prices and terms and conditions for domestic shipments for you as a direct paying customer',
        deliveryscopeheading: 'Deliveries within Sweden only',
        resultlistheading: 'Some suitable options for you',
        weightlabel: 'Weight',
        maxweightlabel: 'Max weight',
        maxmeasurementlabel: 'Maximum measurement',
        senddirectcta: 'Buy Online',
        stampshopcta: 'Buy Stamps',
        message: 'When buying from any agent',
        servicePointHeader: 'Or find nearest service point',
        servicePointLink: 'Search letter-box or service point',
    },
    da: {
        componentheading: 'Vad vill du skicka?',
        deliveryscopeheading: 'Kun leveringer indenfor Sverige',
        resultlistheading: 'Some suitable options for you',
        weightlabel: 'Vægt',
        maxweightlabel: 'Max vægt',
        maxmeasurementlabel: 'Maks. Volumen',
        senddirectcta: 'Buy shipping in Send Direct',
        stampshopcta: 'Order home stamps from the webshop',
    },
    no: {
        componentheading: 'Vad vill du skicka?',
        deliveryscopeheading: 'Kun leveranser innen Sverige',
        resultlistheading: 'Some suitable options for you',
        weightlabel: 'Vekt',
        maxweightlabel: 'Maks vekt',
        maxmeasurementlabel: 'Maks volum',
        senddirectcta: 'Buy shipping in Send Direct',
        stampshopcta: 'Order home stamps from the webshop',
    },
    fi: {
        componentheading: 'Vad vill du skicka?',
        deliveryscopeheading: 'Toimitukset vain Ruotsin sisällä',
        resultlistheading: 'Some suitable options for you',
        weightlabel: 'Paino',
        maxweightlabel: 'Max paino',
        maxmeasurementlabel: 'Max äänenvoimakkuus',
        senddirectcta: 'Buy shipping in Send Direct',
        stampshopcta: 'Order home stamps from the webshop',
    },
};
const markets = {
    se: {
        currency: 'kr',
    },
    dk: {
        currency: 'dk',
    },
    fi: {
        currency: ' €',
    },
    no: {
        currency: 'kr',
    },
};

const pnProductPricelistCss = "pn-button-dropdown .pn-button-dropdown>pn-button>.pn-button{min-height:initial}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}@media screen and (max-width: 767px){.sr-only-mobile{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0, 0, 0, 0);white-space:nowrap;border-width:0}}pn-product-pricelist pn-spinner{margin:1em auto}pn-product-pricelist h2,pn-product-pricelist .heading{text-align:center;font-size:1.5em;line-height:1.1;font-weight:bold;margin-bottom:1rem}pn-product-pricelist .content,pn-product-pricelist .weightselection,pn-product-pricelist .resultlist{max-width:50em;margin:0 auto 1.6rem}pn-product-pricelist .weightoption{display:flex;gap:1.6rem}pn-product-pricelist .weightoption-input{width:5em;position:relative;display:none}pn-product-pricelist .weightoption-input input::-webkit-outer-spin-button,pn-product-pricelist .weightoption-input input::-webkit-inner-spin-button{-webkit-appearance:none;margin:0}pn-product-pricelist .weightoption-input-abbreviation{position:absolute;top:50%;transform:translateY(-50%);right:1.2em;margin-top:1.55rem;font-weight:500}pn-product-pricelist .weightoption-range{flex-grow:1;align-self:flex-end;position:relative;top:0.5em}pn-product-pricelist .weightoption-range input{width:calc(100% - 1.5em)}pn-product-pricelist .weightoption-range datalist{width:100%;display:flex;justify-content:space-between;font-size:0.75em}pn-product-pricelist .resultlist:not([data-list-count=\"0\"]){margin-top:2em}";
const PnProductPricelistStyle0 = pnProductPricelistCss;

const PnFindProductPricelist = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.source = 'https://com-inte.postnord.com/globalassets/global/prices/sv-findprice-1-PN.xlsx';
        this.language = 'sv';
        this.market = 'se';
        this.productid = '';
        this.filteredItems = [];
        this.sourceData = { types: [], data: [] };
        this.gotData = false;
        this.loading = false;
        this.postagetype = { value: '' };
        this.weight = { value: 0, name: '' };
        this.weightvalue = 0;
    }
    componentWillLoad() {
        this.setState();
        this.getDataSource().then(() => { });
    }
    setState() {
        if (this.market) {
            state.market = markets[this.market];
        }
        if (this.language && translations[this.language]) {
            state.i18n = translations[this.language];
        }
    }
    async getDataSource() {
        if (!this.productid) {
            return;
        }
        this.loading = true;
        const cacheBusting = `?cache=${Math.floor(Math.random() * (10000 - 1) + 1)}`;
        const req = await fetch(this.source + cacheBusting);
        const reqbody = await req.blob();
        try {
            const convertedData = await convertFileFromStream(reqbody);
            this.sourceData = convertedData;
            this.filterItems();
        }
        catch (e) {
            this.loading = false;
            console.warn('Unable to parse data');
        }
    }
    filterItems() {
        if (!this.sourceData?.data) {
            console.info('Data has not yet been loaded');
            return;
        }
        this.loading = false;
        const productIdItems = this.sourceData.data.filter(x => x.id === this.productid);
        if (productIdItems.length === 0) {
            return;
        }
        this.postagetype.value = productIdItems[0].what;
        if (!weights[this.postagetype.value] || weights[this.postagetype.value].length === 0) {
            return;
        }
        if (this.weightvalue === 0) {
            this.weight = weights[this.postagetype.value][0];
            this.weightvalue = this.weight.value;
        }
        // For letters we use grams. Otherwise we use KG
        const comparisonWeight = this.weight.abbreviation.toLocaleLowerCase() === 'kg' && this.postagetype.value === 'letter' ? this.weight.value * 1000 : this.weight.value;
        let weightFilteredData = productIdItems.filter(x => x.maxweight === comparisonWeight);
        // In parcels the maxwidths don't necessarily start at 1kg so we fetch any type below the user selected value
        if (weightFilteredData.length === 0) {
            weightFilteredData = productIdItems.filter(x => x.maxweight >= comparisonWeight);
            if (weightFilteredData.length > 0) {
                // We reverse the result array since the map will take the last unique item instead of the first
                const uniqueWeightOptions = [...new Map(weightFilteredData.reverse().map(item => [item['id'], item])).values()];
                if (uniqueWeightOptions.length > 0) {
                    weightFilteredData = uniqueWeightOptions;
                }
            }
        }
        this.filteredItems = weightFilteredData;
        this.gotData = this.filteredItems.length > 0;
    }
    render() {
        return (h(Host, { key: 'bc5342bd9146d1a2c4e6f7452a3819f6d52bf1d8' }, h("div", { key: '1b36f58ccf4039e8f627d399771833771915a66b', class: "content" }, h("slot", { key: '718cd122baaa9be3e59bccab68557d74f63f515b' })), this.loading ? h("pn-spinner", null) : null, this.postagetype.value && weights[this.postagetype.value] ? (h("div", { class: "weightselection" }, h("div", { class: "weightoption" }, h("div", { class: "weightoption-input" }, h("pn-input", { inputid: "weightvalue", name: "weightvalue", type: "number", label: state.i18n.weightlabel, value: this.weightvalue, onChange: e => {
                if (e && e.target && e.target.value) {
                    const weightNum = parseInt(e.target.value, 10);
                    if (!isNaN(weightNum)) {
                        let matchingWeights = weights[this.postagetype.value].filter(x => {
                            let weightInGrams = x.abbreviation === 'kg' ? x.value * 1000 : x.value;
                            return weightInGrams >= weightNum;
                        });
                        if (matchingWeights && matchingWeights.length > 0) {
                            this.weight = matchingWeights[0];
                            this.weightvalue = this.weight.value;
                        }
                    }
                }
            } }), h("span", { class: "weightoption-input-abbreviation" }, this.weight.abbreviation)), h("div", { class: "weightoption-range" }, h("input", { type: "range", id: "weightrange", name: "weightrange", list: "weightlist", value: !this.weight.value ? 0 : weights[this.postagetype.value].findIndex(x => x.value === this.weightvalue), onChange: e => {
                const value = e.target.value;
                this.weight = weights[this.postagetype.value][value];
                this.weightvalue = this.weight.value;
            }, min: "0",
            // max={ weights[this.postagetype.value][weights[this.postagetype.value].length - 1].value }
            max: weights[this.postagetype.value].length - 1 }), h("datalist", { id: "weightlist" }, weights[this.postagetype.value].map((weight, weightindex) => {
            let labelText = weight.name;
            if (weights[this.postagetype.value].length > 6) {
                labelText = weightindex % 5 === 0 || weightindex === 0 || weightindex === weights[this.postagetype.value].length - 1 ? weight.name : '';
            }
            return h("option", { value: weightindex, ...(labelText ? { label: labelText } : {}) });
        })))))) : null, h("div", { key: '1276f72147dd62396be53a819fc9d393f3b5af6b', class: "resultlist", "data-list-count": this.filteredItems.length }, this.filteredItems.map((item) => (h("div", null, h("pn-find-price-result", { item: item })))))));
    }
    static get watchers() { return {
        "market": ["setState"],
        "language": ["setState"],
        "postagetype": ["filterItems"],
        "weightvalue": ["filterItems"]
    }; }
};
PnFindProductPricelist.style = PnProductPricelistStyle0;

export { PnFindProductPricelist as pn_product_pricelist };

//# sourceMappingURL=pn-product-pricelist.entry.js.map