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

export class LaundryOrderAttributePopup extends Component {
    static components = { Dialog };
    static template = "laundry_management.LaundryOrderAttributePopup";
    static props = {
        attributes: { type: Array },
        preselectedIds: { type: Array, optional: true },
        title: { type: String, optional: true },
        getPayload: { type: Function },
        close: { type: Function },
    };
    static defaultProps = {
        preselectedIds: [],
        title: _t("Select Attributes"),
    };

    setup() {
        const initial = new Set(this.props.preselectedIds);
        this.state = useState({
            selected: initial,
        });
    }

    toggle(attrId) {
        if (this.state.selected.has(attrId)) {
            this.state.selected.delete(attrId);
        } else {
            this.state.selected.add(attrId);
        }
    }

    isSelected(attrId) {
        return this.state.selected.has(attrId);
    }

    confirm() {
        const ids = [...this.state.selected];
        const chosen = this.props.attributes.filter((a) => ids.includes(a.id));
        this.props.getPayload(chosen);
        this.props.close();
    }

    skip() {
        this.props.getPayload([]);
        this.props.close();
    }
}
