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

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

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

const SallaEditOrderButton = class {
    constructor(hostRef) {
        index.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 (index.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 index.h(index.Host, null, this.renderEditButton());
        }
        if (!this.paymentUrl)
            return null;
        return (index.h(index.Host, null, index.h("salla-button", { href: this.paymentUrl, color: "primary", size: "small" }, index.h("i", { class: "sicon-wallet" }), salla.lang.getWithDefault('pages.orders.pay_difference', 'دفع فارق المبلغ'))));
    }
    static get watchers() { return {
        "orderId": ["onOrderIdChange"]
    }; }
};
SallaEditOrderButton.style = sallaEditOrderButtonCss;

exports.salla_edit_order_button = SallaEditOrderButton;
