/*!
 * Crafted with ❤ by Salla
 */
import { proxyCustomElement, HTMLElement, h, Host } from '@stencil/core/internal/client';
import { d as defineCustomElement$3 } from './salla-button2.js';
import { d as defineCustomElement$2 } from './salla-count-down2.js';

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

const SallaEditOrderButton$1 = /*@__PURE__*/ proxyCustomElement(class SallaEditOrderButton extends HTMLElement {
    constructor() {
        super();
        this.__registerHost();
        this.order = null;
        this.loading = true;
    }
    async componentWillLoad() {
        await salla.onReady();
        await this.loadOrder();
    }
    async onOrderIdChange() {
        this.loading = true;
        this.order = null;
        await this.loadOrder();
    }
    get paymentUrl() {
        return this.order?.customer_editing?.payment_url;
    }
    async loadOrder() {
        const orderId = this.orderId || salla.config.get('page.id');
        if (!orderId) {
            this.loading = false;
            return;
        }
        try {
            const response = await salla.order.api.getDetails(orderId);
            this.order = response?.data ?? response ?? null;
        }
        catch (error) {
            salla.logger?.warn?.('[salla-edit-order-button] failed to fetch order details', error);
            this.order = null;
        }
        finally {
            this.loading = false;
        }
    }
    renderEditButton() {
        return (h("salla-count-down", { prefixText: salla.lang.getWithDefault('pages.orders.edit_within', 'يمكنك التعديل خلال'), buttonHref: this.order?.edit_url, buttonText: salla.lang.getWithDefault('pages.orders.edit_order', 'تعديل الطلب'), buttonIcon: "sicon-edit", date: this.order?.editable_until, horizontal: true, autoSegments: true, withButton: true, labeled: false, size: "sm", color: "primary" }));
    }
    render() {
        if (this.loading || !this.order)
            return null;
        if (this.order?.is_editable) {
            return h(Host, null, this.renderEditButton());
        }
        if (!this.paymentUrl)
            return null;
        return (h(Host, null, h("salla-button", { href: this.paymentUrl, color: "primary", size: "small" }, h("i", { class: "sicon-wallet" }), salla.lang.getWithDefault('pages.orders.pay_difference', 'دفع فارق المبلغ'))));
    }
    static get watchers() { return {
        "orderId": ["onOrderIdChange"]
    }; }
    static get style() { return sallaEditOrderButtonCss; }
}, [0, "salla-edit-order-button", {
        "orderId": [8, "order-id"],
        "order": [32],
        "loading": [32]
    }, undefined, {
        "orderId": ["onOrderIdChange"]
    }]);
function defineCustomElement$1() {
    if (typeof customElements === "undefined") {
        return;
    }
    const components = ["salla-edit-order-button", "salla-button", "salla-count-down"];
    components.forEach(tagName => { switch (tagName) {
        case "salla-edit-order-button":
            if (!customElements.get(tagName)) {
                customElements.define(tagName, SallaEditOrderButton$1);
            }
            break;
        case "salla-button":
            if (!customElements.get(tagName)) {
                defineCustomElement$3();
            }
            break;
        case "salla-count-down":
            if (!customElements.get(tagName)) {
                defineCustomElement$2();
            }
            break;
    } });
}
defineCustomElement$1();

const SallaEditOrderButton = SallaEditOrderButton$1;
const defineCustomElement = defineCustomElement$1;

export { SallaEditOrderButton, defineCustomElement };
