import {defineStore} from 'pinia'
import {config} from '@/config'
import performRequest from "@/backend.js";

export const useReportSearchStore = defineStore('reportSearch', {
    state() {
        return {
            suppliers: [],
            selectedSuppliers: [],
            remainingSuppliers: [],
            material: null,
            loading: false,
        }
    },
    getters: {
        getSuppliers(state) {
            return state.remainingSuppliers;
        },
        isSelected(state) {
            return function (supplier) {
                return state.selectedSuppliers.some(s => s.id === supplier.id && s.is_user_node === supplier.is_user_node);
            }
        },
        getSelectedSuppliers(state) {
            return state.selectedSuppliers;
        },
        getMaterialId(state) {
            return state.material?.id;
        },
        getMaterial(state) {
            return state.material;
        },
    },
    actions: {
        async reset() {
            this.suppliers = [];
            this.material = null;
            this.selectedSuppliers = [];
            this.remainingSuppliers = [];
        },
        async selectSupplier(supplier) {

            if (!this.selectedSuppliers.some(s => s.id === supplier.id && s.is_user_node === supplier.is_user_node))
                this.selectedSuppliers.push(supplier)
            else {
                const index = this.selectedSuppliers.findIndex(s => s.id === supplier.id && s.is_user_node === supplier.is_user_node);
                this.selectedSuppliers.splice(index, 1);
            }


            this.updateShownSuppliers();
        },
        updateShownSuppliers() {
            const toBeShown = []

            this.suppliers.forEach(supplierList => {
                const shouldInclude = this.selectedSuppliers.every(selSup => supplierList.some(s => s.id === selSup.id && s.is_user_node === selSup.is_user_node))

                if (shouldInclude)
                    toBeShown.push(...supplierList);
            })


            this.remainingSuppliers = toBeShown.filter((item, index) =>
                toBeShown.findIndex(obj => obj.id === item.id && obj.is_user_node === item.is_user_node) === index
            );
        },
        async setMaterial(material) {
            this.material = material;
            await this.updateSuppliers();
        },
        async updateSuppliers() {
            if (this.getMaterialId == null) return;

            this.loading = true;
            this.suppliers = [];
            this.selectedSuppliers = [];
            this.remainingSuppliers = [];

            const params = new URLSearchParams();
            params.append('material', this.getMaterialId);
            const url = `${config.backendUrl}/reports/search/${params.size === 0 ? '' : '?'}${params.toString()}`;

            const resp = await performRequest(this,'GET', url, null).catch(e => {
                this.loading = false;
            });
            this.suppliers = resp.data;

            this.updateShownSuppliers();

            this.loading = false;
        }
    },

});