/** @odoo-module */
import { Component, useState } from "@odoo/owl";
import { Dialog } from "@web/core/dialog/dialog";
import { _t } from "@web/core/l10n/translation";

export class LaundryOrderTypePopup extends Component {
    static components = { Dialog };
    static template = "laundry_management.LaundryOrderTypePopup";
    static props = {
        types: { type: Array },
        defaultTypeId: { type: [Number, Boolean], optional: true },
        showIcons: { type: Boolean, optional: true },
        allowSkip: { type: Boolean, optional: true },
        title: { type: String, optional: true },
        getPayload: { type: Function },
        close: { type: Function },
    };
    static defaultProps = {
        showIcons: true,
        allowSkip: true,
        title: _t("Select Order Type"),
    };

    setup() {
        this.state = useState({
            selectedId: this.props.defaultTypeId || false,
        });
    }

    select(typeId) {
        this.state.selectedId = typeId;
    }

    confirm() {
        if (!this.state.selectedId && !this.props.allowSkip) {
            return;
        }
        const chosen = this.props.types.find((t) => t.id === this.state.selectedId);
        this.props.getPayload(chosen || null);
        this.props.close();
    }

    skip() {
        this.props.getPayload(null);
        this.props.close();
    }

    cancel() {
        this.props.close();
    }
}
