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

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

const DEBUG_KEY = 'salla-delivery-promise-debug';
function log(message, data) {
    if (localStorage.getItem(DEBUG_KEY)) {
        data !== undefined ? console.log(message, data) : console.log(message);
    }
}
/** Fetch available cities for a product's delivery promises. */
async function fetchCities(productId) {
    log('fetchCities start', { productId });
    const response = await salla.api.request(salla.url.api(`products/${productId}/delivery-promises/cities`));
    if (!response.success || !Array.isArray(response.data) || response.data.length === 0) {
        log('fetchCities: no data', { response });
        return { cities: [], preselected: undefined };
    }
    const preselected = response.data.find((c) => c.is_selected);
    log('fetchCities success', { count: response.data.length, preselected });
    return { cities: response.data, preselected };
}
/** Fetch the delivery promise message for a given city or branch. */
async function fetchDeliveryMessage(productId, id, optionType, isLoginCycleEnabled) {
    log('fetchDeliveryMessage start', { productId, id, optionType });
    let queryString = '';
    if (optionType === 'city' && !isLoginCycleEnabled) {
        queryString = `?reference_id=${id}`;
    }
    else {
        queryString = `?reference_id=${id}&reference_type=${optionType}`;
    }
    const response = (await salla.api.withoutNotifier(() => salla.api.request(salla.url.api(`products/${productId}/delivery-promises${queryString}`))));
    if (!response.success) {
        log('fetchDeliveryMessage error', { response });
        throw new Error(response.error?.message || 'Failed to fetch delivery promise');
    }
    const message = response.data?.message?.trim();
    log('fetchDeliveryMessage success', { message });
    return message || null;
}

/** Filters cities by Arabic/English name (used inside the city-change modal). */
function filterCitiesForModal(cities, query) {
    const q = query.trim().toLowerCase();
    if (!q) {
        return [...cities];
    }
    return cities.filter(city => city.name.toLowerCase().includes(q) || (city.name_en || '').toLowerCase().includes(q));
}

const INTENT_KEY = 'bullet_delivery_intent';
function getIntentStorage() {
    const rememberLastSession = Boolean(salla.config.get('store.settings.bullet_delivery.settings.remember_last_session'));
    return rememberLastSession ? salla.storage.store : salla.storage.session;
}
function getStoredIntent() {
    const raw = getIntentStorage().get(INTENT_KEY);
    return raw && typeof raw === 'object' ? raw : null;
}
/** Resolve the stored intent into a city or branch selection. Returns null when no usable intent. */
function resolveIntent() {
    const intent = getStoredIntent();
    if (!intent)
        return null;
    if (intent.type === 'address') {
        const city = intent.address_details?.city;
        const cityId = city?.id != null ? Number(city.id) : intent.city_id != null ? Number(intent.city_id) : null;
        const cityName = city?.name?.trim() || '';
        if (cityId != null) {
            return { type: 'address', option: { id: cityId, name: cityName } };
        }
        return null;
    }
    if (intent.type === 'branch') {
        const branchId = intent.branch_id != null
            ? Number(intent.branch_id)
            : intent.branch_details?.id != null
                ? Number(intent.branch_details.id)
                : null;
        const branchName = intent.branch_details?.name?.trim() || '';
        if (branchId != null) {
            return { type: 'branch', option: { id: branchId, name: branchName } };
        }
        return null;
    }
    return null;
}
/** Build the payload for opening the bullet delivery modal with preselection. */
function buildBulletOpenPayload() {
    const intent = getStoredIntent();
    const payload = {};
    if (intent?.type === 'address' && intent.address_id != null) {
        payload.preselected_address_id = Number(intent.address_id);
    }
    if (intent?.type === 'branch') {
        const branchId = intent.branch_id ?? intent.branch_details?.id;
        if (branchId != null) {
            payload.preselected_branch_id = Number(branchId);
        }
    }
    return payload;
}

const DEFAULT_LABELS = {
    deliveryTo: 'توصيل إلى',
    pickupFromBranch: 'الاستلام من فرع',
    noResults: 'لا توجد نتائج',
    selectCity: 'اختر المدينة',
    changeCityTitle: 'تغيير المدينة',
    changeCitySubtitle: '',
    cityFieldLabel: 'المدينة',
    modalSearchPlaceholder: 'ابحث عن مدينة',
    confirmAddress: 'تأكيد العنوان',
    errorDeliveryPromise: 'لا يتوفر وعد تسليم لهذه المدينة',
    errorPickupPromise: 'لا يتوفر وعد تسليم لهذا الفرع',
};
function loadLabels() {
    return {
        deliveryTo: salla.lang.getWithDefault('pages.products.promise_deliver_to', DEFAULT_LABELS.deliveryTo),
        pickupFromBranch: salla.lang.getWithDefault('pages.products.promise_pickup_from_branch', DEFAULT_LABELS.pickupFromBranch),
        noResults: salla.lang.getWithDefault('common.elements.no_options', DEFAULT_LABELS.noResults),
        selectCity: salla.lang.getWithDefault('common.elements.select_city', DEFAULT_LABELS.selectCity),
        changeCityTitle: salla.lang.getWithDefault('pages.products.promise_change_city_title', DEFAULT_LABELS.changeCityTitle),
        changeCitySubtitle: salla.lang.getWithDefault('pages.products.promise_change_city_subtitle', 'قد تتغيّر مدة التوصيل حسب المدينة.'),
        cityFieldLabel: salla.lang.getWithDefault('pages.products.promise_city_field', DEFAULT_LABELS.cityFieldLabel),
        modalSearchPlaceholder: salla.lang.getWithDefault('pages.products.promise_search_city', DEFAULT_LABELS.modalSearchPlaceholder),
        confirmAddress: salla.lang.getWithDefault('pages.checkout.confirm_address', DEFAULT_LABELS.confirmAddress),
        errorDeliveryPromise: salla.lang.getWithDefault('pages.products.promise_delivery_not_available', DEFAULT_LABELS.errorDeliveryPromise),
        errorPickupPromise: salla.lang.getWithDefault('pages.products.promise_pickup_not_available', DEFAULT_LABELS.errorPickupPromise),
    };
}
/** Localized city display name (English when available and user language is not Arabic). */
function getCityDisplayName(city) {
    const lang = salla.config.get('user.language_code');
    if (lang && lang !== 'ar' && city.name_en?.trim()) {
        return city.name_en.trim();
    }
    return city.name;
}

const SallaDeliveryPromise = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        // ── Feature flags & config ────────────────────────────────────────────
        this.isDeliveryPromiseEnabled = false;
        this.isLoginCycleEnabled = false;
        this.canRender = false;
        // ── Shared display state ──────────────────────────────────────────────
        this.labels = DEFAULT_LABELS;
        this.selectedLabel = null;
        this.deliveryMessage = null;
        this.hasError = false;
        this.errorMessage = '';
        this.isLoadingPromises = false;
        // ── Login-cycle state (bullet delivery integration) ───────────────────
        this.selectedCityLoginCycle = null;
        this.selectedBranchLoginCycle = null;
        // ── Standard flow state (city list) ───────────────────────────────────
        this.cities = [];
        this.selectedCity = null;
        this.isLoadingCities = false;
        // ── City-change modal state (non–login-cycle) ─────────────────────────
        this.modalSearchQuery = '';
        this.cityPendingSelection = null;
        this.isCityPopoverOpen = false;
        // ═══════════════════════════════════════════════════════════════════════
        //  Login-cycle (bullet delivery) integration
        // ═══════════════════════════════════════════════════════════════════════
        this.onBulletDeliveryConfirmed = () => {
            this.applyBulletIntent();
        };
        this.handleModalSearchInput = (event) => {
            this.modalSearchQuery = event.target.value;
            this.isCityPopoverOpen = true;
        };
        this.handleModalComboboxActivate = () => {
            this.isCityPopoverOpen = true;
        };
        // ═══════════════════════════════════════════════════════════════════════
        //  Header click (dispatches to correct flow)
        // ═══════════════════════════════════════════════════════════════════════
        this.handleHeaderClick = (e) => {
            e.stopPropagation();
            if (this.isLoginCycleEnabled) {
                this.openBulletDeliveryModal();
            }
            else {
                void this.openCityChangeModal();
            }
        };
    }
    // ═══════════════════════════════════════════════════════════════════════
    //  Lifecycle
    // ═══════════════════════════════════════════════════════════════════════
    async componentDidLoad() {
        await salla.onReady();
        await salla.lang.onLoaded();
        this.labels = loadLabels();
        this.isDeliveryPromiseEnabled = salla.config.get('store.features', []).includes('delivery-promises');
        this.isLoginCycleEnabled = salla.config.get('store.features', []).includes('bullet-delivery-v2');
        this.productId = salla.config.get('page.id');
        if (!this.isDeliveryPromiseEnabled) {
            this.canRender = false;
            return;
        }
        if (this.isLoginCycleEnabled) {
            this.applyBulletIntent();
            this.canRender = true;
            window.addEventListener('bulletDeliveryConfirmed', this.onBulletDeliveryConfirmed);
        }
        else {
            this.selectedLabel = `${this.labels.deliveryTo} ${this.labels.selectCity}`;
            await this.loadCities();
        }
    }
    disconnectedCallback() {
        window.removeEventListener('bulletDeliveryConfirmed', this.onBulletDeliveryConfirmed);
    }
    applyBulletIntent() {
        const resolved = resolveIntent();
        if (!resolved) {
            this.selectedLabel = `${this.labels.deliveryTo} ${this.labels.selectCity}`;
            this.selectedCityLoginCycle = null;
            this.selectedBranchLoginCycle = null;
            this.deliveryMessage = null;
            return;
        }
        if (resolved.type === 'address') {
            this.selectedCityLoginCycle = resolved.option;
            this.selectedBranchLoginCycle = null;
            this.selectedLabel = `${this.labels.deliveryTo} ${resolved.option.name || this.labels.selectCity}`;
            void this.loadDeliveryMessage(resolved.option.id, 'city');
        }
        else {
            this.selectedBranchLoginCycle = resolved.option;
            this.selectedCityLoginCycle = null;
            this.selectedLabel = `${this.labels.pickupFromBranch} ${resolved.option.name}`;
            void this.loadDeliveryMessage(resolved.option.id, 'branch');
        }
    }
    openBulletDeliveryModal() {
        salla.event.emit('salla::bullet-delivery.modal.open.requested', buildBulletOpenPayload());
    }
    // ═══════════════════════════════════════════════════════════════════════
    //  Standard flow (city list, no login cycle)
    // ═══════════════════════════════════════════════════════════════════════
    async loadCities() {
        this.isLoadingCities = true;
        this.hasError = false;
        try {
            const { cities, preselected } = await fetchCities(this.productId);
            if (cities.length === 0) {
                this.canRender = false;
                return;
            }
            this.cities = cities;
            if (preselected) {
                this.selectedCity = preselected;
                this.selectedLabel = `${this.labels.deliveryTo} ${getCityDisplayName(preselected)}`;
                await this.loadDeliveryMessage(preselected.id, 'city');
            }
            this.canRender = true;
        }
        catch (error) {
            this.hasError = true;
            this.errorMessage = error.message || 'Failed to load cities';
            this.canRender = false;
        }
        finally {
            this.isLoadingCities = false;
        }
    }
    // ═══════════════════════════════════════════════════════════════════════
    //  Delivery message (shared by both flows)
    // ═══════════════════════════════════════════════════════════════════════
    async loadDeliveryMessage(id, optionType) {
        this.isLoadingPromises = true;
        this.hasError = false;
        this.deliveryMessage = null;
        try {
            this.deliveryMessage = await fetchDeliveryMessage(this.productId, id, optionType, this.isLoginCycleEnabled);
        }
        catch (error) {
            this.hasError = true;
            this.errorMessage = optionType === 'city' ? this.labels.errorDeliveryPromise : this.labels.errorPickupPromise;
        }
        finally {
            this.isLoadingPromises = false;
        }
    }
    // ═══════════════════════════════════════════════════════════════════════
    //  City-change modal handlers (non–login-cycle)
    // ═══════════════════════════════════════════════════════════════════════
    async openCityChangeModal() {
        if (!this.cityModalRef)
            return;
        this.cityPendingSelection = null;
        this.modalSearchQuery = '';
        this.isCityPopoverOpen = false;
        await this.cityModalRef.setTitle(this.labels.changeCityTitle);
        await this.cityModalRef.open();
    }
    handleModalSelectCity(city) {
        this.cityPendingSelection = city;
        this.modalSearchQuery = getCityDisplayName(city);
        this.isCityPopoverOpen = false;
    }
    async handleConfirmCityModal() {
        if (!this.cityPendingSelection) {
            await this.cityModalRef?.close();
            return;
        }
        const city = this.cityPendingSelection;
        this.selectedCity = city;
        this.selectedLabel = `${this.labels.deliveryTo} ${getCityDisplayName(city)}`;
        await this.loadDeliveryMessage(city.id, 'city');
        await this.cityModalRef?.close();
    }
    // ═══════════════════════════════════════════════════════════════════════
    //  Render helpers
    // ═══════════════════════════════════════════════════════════════════════
    renderLoadingSkeleton() {
        return (index.h(index.Host, { class: "s-delivery-promise-wrapper s-delivery-promise-skeleton" }, index.h("div", { class: "s-delivery-promise-container" }, index.h("div", { class: "s-delivery-promise-header s-delivery-promise-header-skeleton" }, index.h("div", { class: "s-delivery-promise-location" }, index.h("salla-skeleton", { height: "14px", width: "180px" })), index.h("salla-skeleton", { height: "18px", width: "18px" })), index.h("div", { class: "s-delivery-promise-loading" }, index.h("salla-skeleton", { height: "20px", width: "80%" })))));
    }
    renderDeliveryMessage() {
        if (this.isLoadingPromises) {
            return (index.h("div", { class: "s-delivery-promise-loading" }, index.h("salla-skeleton", { height: "20px", width: "80%" })));
        }
        if (!this.deliveryMessage)
            return null;
        return index.h("div", { class: "s-delivery-promise-message" }, this.deliveryMessage);
    }
    renderCityModalBody() {
        const filtered = filterCitiesForModal(this.cities, this.modalSearchQuery);
        return (index.h("div", { class: "s-delivery-promise-modal-body" }, index.h("label", { class: "s-delivery-promise-modal-field-label", htmlFor: "s-delivery-promise-modal-search" }, this.labels.cityFieldLabel, index.h("span", { class: "s-delivery-promise-required", "aria-hidden": "true" }, " *")), index.h("div", { class: "s-delivery-promise-modal-combobox" }, index.h("div", { class: "s-delivery-promise-modal-search", onClick: this.handleModalComboboxActivate }, index.h("i", { class: "sicon-search", "aria-hidden": "true" }), index.h("input", { id: "s-delivery-promise-modal-search", type: "text", class: "form-input", placeholder: this.labels.modalSearchPlaceholder, value: this.modalSearchQuery, onInput: this.handleModalSearchInput, autocomplete: "off", "aria-autocomplete": "list", "aria-expanded": this.isCityPopoverOpen ? 'true' : 'false', "aria-controls": "s-delivery-promise-modal-listbox" })), index.h("div", { id: "s-delivery-promise-modal-listbox", class: "s-delivery-promise-modal-popover", role: "listbox", hidden: !this.isCityPopoverOpen }, index.h("div", { class: "s-delivery-promise-modal-list s-scrollbar" }, filtered.length === 0 ? (index.h("div", { class: "s-delivery-promise-modal-empty" }, this.labels.noResults)) : (filtered.map(city => (index.h("button", { key: city.id, type: "button", role: "option", class: {
                's-delivery-promise-modal-item': true,
                's-delivery-promise-modal-item--selected': this.cityPendingSelection?.id === city.id,
            }, "aria-selected": this.cityPendingSelection?.id === city.id ? 'true' : 'false', onClick: e => {
                e.stopPropagation();
                this.handleModalSelectCity(city);
            } }, index.h("span", { class: "s-delivery-promise-modal-item-name" }, city.name), this.cityPendingSelection?.id === city.id ? index.h("i", { class: "sicon-check", "aria-hidden": "true" }) : null)))))))));
    }
    renderCityChangeModal() {
        if (this.isLoginCycleEnabled)
            return null;
        return (index.h("salla-modal", { id: "s-delivery-promise-city-modal", ref: el => { this.cityModalRef = el; }, class: "s-delivery-promise-city-modal", isClosable: true, width: "md", "modal-title": this.labels.changeCityTitle, subTitle: this.labels.changeCitySubtitle }, this.renderCityModalBody(), index.h("salla-button", { slot: "footer", width: "wide", disabled: !this.cityPendingSelection, onClick: () => this.handleConfirmCityModal() }, !this.isLoadingPromises && this.labels.confirmAddress)));
    }
    // ═══════════════════════════════════════════════════════════════════════
    //  Main render
    // ═══════════════════════════════════════════════════════════════════════
    render() {
        if (!this.isDeliveryPromiseEnabled)
            return null;
        if (this.isLoadingCities)
            return this.renderLoadingSkeleton();
        if (!this.canRender)
            return null;
        return (index.h(index.Host, { class: "s-delivery-promise-wrapper" }, index.h("div", { class: "s-delivery-promise-container" }, index.h("div", { class: "s-delivery-promise-header", onClick: this.handleHeaderClick }, index.h("div", { class: "s-delivery-promise-location" }, index.h("span", { class: "s-delivery-promise-title" }, this.selectedLabel)), index.h("i", { class: `sicon-keyboard_arrow_down s-delivery-promise-arrow` })), this.hasError && index.h("div", { class: "s-delivery-promise-error" }, this.errorMessage), !this.hasError && this.renderDeliveryMessage()), this.renderCityChangeModal()));
    }
    get host() { return index.getElement(this); }
};

exports.salla_delivery_promise = SallaDeliveryPromise;
