/*!
 * Crafted with ❤ by Salla
 */
import { r as registerInstance, h, H as Host } from './index-Cb4NFvMH.js';

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

const SallaEditOrderButton = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        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"]
    }; }
};
SallaEditOrderButton.style = sallaEditOrderButtonCss;

export { SallaEditOrderButton as salla_edit_order_button };
