import { ApplicationModule } from './app.js';
import { addFlashField, setupFlash } from '../../plugins/flash/flash.js';
import { _ } from '../../i18n/index.js';
import { CollidableField } from './blockly/collidable-field.js';
export const AppAPI = (editor) => ({
    type: 'module',
    name: ApplicationModule.id,
    color: '#5fc9f3',
    verbose: _('MODULE_APP', 'App'),
    symbols: [{
            type: 'function',
            name: 'onStart',
            verbose: _('WHEN_APP_STARTS', 'when app starts'),
            parameters: [{
                    type: 'parameter',
                    name: 'callback',
                    verbose: '',
                    returnType: Function,
                }],
            blockly: {
                postProcess(block) {
                    addFlashField(block);
                    block.setPreviousStatement(false);
                    block.setNextStatement(false);
                },
            },
        }, {
            type: 'function',
            name: 'onPartCollision',
            verbose: _('WHILE', 'While'),
            parameters: [{
                    type: 'parameter',
                    name: 'a',
                    verbose: '',
                    returnType: 'Part',
                    blockly: {
                        customField(Blockly, block) {
                            const field = new CollidableField('', () => {
                                const parts = editor.parts.getAddedParts();
                                const items = [];
                                parts.forEach((partRecord, id) => items.push({ id, label: partRecord.part.name || '' }));
                                return items;
                            });
                            editor.parts.onDidAddPart(() => {
                                field.refresh();
                            });
                            return field;
                        },
                    },
                }, {
                    type: 'parameter',
                    name: 'b',
                    verbose: 'collides with',
                    returnType: 'Part',
                    blockly: {
                        customField(Blockly, block) {
                            const field = new CollidableField('', () => {
                                const parts = editor.parts.getAddedParts();
                                const items = [];
                                const a = block.getFieldValue('A');
                                parts.forEach((partRecord, id) => items.push({ id, label: partRecord.part.name || '' }));
                                return items.filter(item => item.id !== a);
                            });
                            editor.parts.onDidAddPart(() => {
                                field.refresh();
                            });
                            return field;
                        },
                    },
                }, {
                    type: 'parameter',
                    name: 'callback',
                    verbose: '',
                    returnType: Function,
                }],
            blockly: {
                postProcess(block) {
                    addFlashField(block);
                    block.setPreviousStatement(false);
                    block.setNextStatement(false);
                },
            },
        }],
    onInstall(editor, mod) {
        setupFlash(editor, ApplicationModule.id, mod._onDidPartCollision, 'onPartCollision', (block, collision) => {
            const aId = block.getFieldValue('A');
            const bId = block.getFieldValue('B');
            return aId === collision.a.id && bId === collision.b.id;
        });
    },
});
//# sourceMappingURL=api.js.map