/** @odoo-module */
import { patch } from "@web/core/utils/patch";
import { Order } from "@pos_preparation_display/app/components/order/order";
import { useService } from "@web/core/utils/hooks";
import { PreparationDisplay } from "@pos_preparation_display/app/models/preparation_display";

patch(PreparationDisplay.prototype, {
    //override the setup of preparation display
    async setup(data, env, preparationDisplayId) {
        this.id = preparationDisplayId;
        this.env = env;
        this.showCategoryFilter = false;
        this.orm = env.services.orm;
        this.orders = {};
        this.orderlines = {};
        this.categories = {};
        this.products = {};
        this.stages = new Map(); // We need a Map() and not an object because the order of the elements is important
        this.selectedStageId = 0;
        this.selectedCategories = new Set();
        this.selectedProducts = new Set();
        this.filteredOrders = [];
        this.noteByLines = {};
        this.rawData = {
            categories: data.categories,
            orders: data.orders,
            stages: data.stages,
            attributes: data.attributes,
            attributeValues: data.attribute_values,
        };

        this.restoreFilterFromLocalStorage();
        this.processStages();
        this.processCategories();
        this.processOrders();
        this.attributes = data.attributes;
        this.attributeValues = data.attribute_values;
        this.posHasProducts = await this.loadPosHasProducts();
        this.loadingProducts = false;

        // force refresh every 5 seconds

        // setTimeout(function(){
        //     window.location.reload();
        // }, 5000);
    },
});