import {defineStore} from "pinia";
import {processBlock} from "@/utils/ftch.js";

export const useBlocksStore = defineStore('blocks', {
    state: () => ({
        blocks: [],
    }),

    actions: {
        async processBlocks(rawBlocks) {
            const results = await Promise.allSettled(
                rawBlocks.map(block => {
                    console.debug('[Blocks Store]: Process block ', block);
                    return processBlock(block)
                        .then(response => response);
                })
            );

            this.blocks = results
                .map(r => {
                    if (r.status === 'fulfilled') {
                        return r.value.data;
                    } else {
                        return {
                            is_enabled: true,
                            type: 'error',
                        };
                    }
                });
        }
    },
});
