"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const xstate_1 = require("xstate");
const createResolveColumnsMachine = (props) => (xstate_1.createMachine({
    id: 'resolveColumns',
    initial: 'idle',
    context: {
        tableName: props.name,
        addedColumns: props.added,
        deletedColumns: props.deleted,
        missingItemIndex: 0,
        newItemIndex: 0,
        created: [],
        renamed: [],
        deleted: [],
    },
    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: '#resolveColumns.confirmationDelete', RENAMED: 'rename' },
                },
                rename: {
                    on: {
                        CHOICE_NEW_ITEM: { target: '#resolveColumns.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: ({ deletedColumns, addedColumns }) => !deletedColumns.length || !addedColumns.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({
            deleted: ({ missingItemIndex, deleted, deletedColumns, }) => [...deleted, deletedColumns[missingItemIndex]],
            deletedColumns: ({ missingItemIndex, deletedColumns, }) => deletedColumns.filter((_, index) => index !== missingItemIndex),
        }),
        rename: xstate_1.assign({
            renamed: ({ missingItemIndex, newItemIndex, renamed, addedColumns, deletedColumns, }) => [
                ...renamed,
                { from: deletedColumns[missingItemIndex], to: addedColumns[newItemIndex] },
            ],
            deletedColumns: ({ missingItemIndex, deletedColumns, }) => deletedColumns.filter((_, index) => index !== missingItemIndex),
            addedColumns: ({ newItemIndex, addedColumns, }) => addedColumns.filter((_, index) => index !== newItemIndex),
        }),
        resolveRemaining: xstate_1.assign({
            created: ({ addedColumns, created, }) => [...created, ...addedColumns],
            deleted: ({ deletedColumns, deleted, }) => [...deleted, ...deletedColumns],
            deletedColumns: (context) => [],
            addedColumns: (context) => [],
        }),
    },
}));
exports.default = createResolveColumnsMachine;
//# sourceMappingURL=resolveColumnsMachine.js.map