"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const xstate_1 = require("xstate");
const createResolveTablesMachine = (props) => (xstate_1.createMachine({
    id: 'resolveTables',
    initial: 'idle',
    context: {
        ...props,
        missingItemIndex: 0,
        newItemIndex: 0,
        createdTables: [],
        renamedTables: [],
        deletedTables: [],
    },
    states: {
        idle: {
            entry: xstate_1.send({ type: 'NEXT' }),
            on: {
                NEXT: [
                    {
                        target: 'done',
                        cond: 'isResolved',
                        actions: ['resolveRemaining'],
                    },
                    {
                        target: 'table',
                    },
                ],
            },
        },
        table: {
            on: {
                CHOICE_ITEM: { target: 'action', actions: ['choseItem'] },
            },
        },
        action: {
            initial: 'actionChoice',
            states: {
                actionChoice: {
                    on: { DELETED: '#resolveTables.confirmationDelete', RENAMED: 'rename' },
                },
                rename: {
                    on: {
                        CHOICE_NEW_ITEM: { target: '#resolveTables.confirmationRename', actions: ['choseNewItem'] },
                    },
                },
            },
        },
        confirmationDelete: {
            on: {
                CANCEL: 'action.actionChoice',
                CONFIRM: [
                    { target: 'check', actions: ['delete'] },
                ],
            },
        },
        confirmationRename: {
            on: {
                CANCEL: 'action.actionChoice',
                CONFIRM: [
                    { target: 'check', actions: ['rename'] },
                ],
            },
        },
        check: {
            entry: xstate_1.send({ type: 'NEXT' }),
            on: {
                NEXT: [
                    {
                        target: 'done',
                        cond: 'isResolved',
                        actions: ['resolveRemaining'],
                    },
                    {
                        target: 'table',
                    },
                ],
            },
        },
        done: {},
    },
}, {
    guards: {
        isResolved: ({ missingTables, newTables }) => !missingTables.length || !newTables.length,
    },
    actions: {
        choseItem: xstate_1.assign({
            missingItemIndex: (context, event) => (event.type === 'CHOICE_ITEM' ? event.itemIndex : 0),
        }),
        choseNewItem: xstate_1.assign({
            newItemIndex: (context, event) => (event.type === 'CHOICE_NEW_ITEM' ? event.itemIndex : 0),
        }),
        delete: xstate_1.assign({
            deletedTables: ({ missingItemIndex, deletedTables, missingTables, }) => [...deletedTables, missingTables[missingItemIndex]],
            missingTables: ({ missingItemIndex, missingTables, }) => missingTables.filter((_, index) => index !== missingItemIndex),
        }),
        rename: xstate_1.assign({
            renamedTables: ({ missingItemIndex, newItemIndex, renamedTables, newTables, missingTables, }) => [
                ...renamedTables,
                { from: missingTables[missingItemIndex], to: newTables[newItemIndex] },
            ],
            missingTables: ({ missingItemIndex, missingTables, }) => missingTables.filter((_, index) => index !== missingItemIndex),
            newTables: ({ newItemIndex, newTables, }) => newTables.filter((_, index) => index !== newItemIndex),
        }),
        resolveRemaining: xstate_1.assign({
            createdTables: ({ newTables, createdTables, }) => [...createdTables, ...newTables],
            deletedTables: ({ missingTables, deletedTables, }) => [...deletedTables, ...missingTables],
            missingTables: (context) => [],
            newTables: (context) => [],
        }),
    }
}));
exports.default = createResolveTablesMachine;
//# sourceMappingURL=resolveTablesMachine.js.map