"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const sinon = require("sinon");
const rules_1 = require("./rules");
const assert = require("assert");
describe('rules', () => {
    let mynahUi;
    let messager;
    let openTopBarButtonOverlayStub;
    let showCustomFormStub;
    let rulesList;
    beforeEach(() => {
        mynahUi = {
            openTopBarButtonOverlay: sinon.stub(),
            showCustomForm: sinon.stub(),
            getAllTabs: sinon.stub().returns({}),
            updateStore: sinon.stub().returns('new-tab-id'),
            notify: sinon.stub(),
        };
        openTopBarButtonOverlayStub = mynahUi.openTopBarButtonOverlay;
        showCustomFormStub = mynahUi.showCustomForm;
        messager = {
            onRuleClick: sinon.stub(),
            onChatPrompt: sinon.stub(),
            onTabAdd: sinon.stub(),
        };
        rulesList = new rules_1.RulesList(mynahUi, messager);
    });
    afterEach(() => {
        sinon.restore();
    });
    describe('showLoading', () => {
        it('opens top bar button overlay with loading message', () => {
            const mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            rulesList.showLoading('test-tab-id');
            sinon.assert.calledOnce(openTopBarButtonOverlayStub);
            const arg = openTopBarButtonOverlayStub.getCall(0).args[0];
            assert.equal(arg.tabId, 'test-tab-id');
            assert.equal(arg.topBarButtonOverlay.list[0].groupName, 'Loading rules...');
            assert.equal(arg.topBarButtonOverlay.selectable, false);
        });
    });
    describe('show', () => {
        it('opens top bar button overlay when called first time', () => {
            const mockParams = {
                tabId: 'test-tab-id',
                rules: [
                    {
                        folderName: 'test-folder',
                        active: true,
                        rules: [
                            {
                                id: 'rule-1',
                                name: 'Test Rule',
                                active: true,
                            },
                        ],
                    },
                ],
                filterOptions: [
                    {
                        id: 'filter-1',
                        type: 'textinput',
                        icon: 'search',
                    },
                ],
            };
            const mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            rulesList.show(mockParams);
            sinon.assert.calledOnce(openTopBarButtonOverlayStub);
            const arg = openTopBarButtonOverlayStub.getCall(0).args[0];
            assert.equal(arg.tabId, 'test-tab-id');
            assert.equal(arg.topBarButtonOverlay.selectable, 'clickable');
        });
        it('updates existing overlay when called second time', () => {
            const mockParams = {
                tabId: 'test-tab-id',
                rules: [],
            };
            const mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            // First call
            rulesList.showLoading('test-tab-id');
            // Second call
            rulesList.show(mockParams);
            sinon.assert.calledOnce(mockOverlay.update);
        });
    });
    describe('rule click handling', () => {
        let mockOverlay;
        let onItemClick;
        beforeEach(() => {
            mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            rulesList.showLoading('test-tab-id');
            onItemClick = openTopBarButtonOverlayStub.getCall(0).args[0].events.onItemClick;
        });
        it('shows custom form when create rule is clicked', () => {
            const createRuleItem = {
                id: rules_1.ContextRule.CreateRuleId,
                description: 'Create a new rule',
            };
            onItemClick(createRuleItem);
            sinon.assert.calledOnce(showCustomFormStub);
            const formArgs = showCustomFormStub.getCall(0).args;
            assert.equal(formArgs[0], 'test-tab-id');
            assert.equal(formArgs[1][0].id, rules_1.ContextRule.RuleNameFieldId);
            assert.equal(formArgs[2][0].id, rules_1.ContextRule.CancelButtonId);
            assert.equal(formArgs[2][1].id, rules_1.ContextRule.SubmitButtonId);
        });
        it('calls messager when create memory bank is clicked', () => {
            const createMemoryBankItem = {
                id: rules_1.ContextRule.CreateMemoryBankId,
                description: 'Generate Memory Bank',
            };
            onItemClick(createMemoryBankItem);
            // Should create new tab and send chat prompt
            sinon.assert.calledOnce(messager.onTabAdd);
            sinon.assert.calledOnce(messager.onChatPrompt);
            const tabAddArgs = messager.onTabAdd.getCall(0).args[0];
            assert.equal(tabAddArgs, 'new-tab-id');
            const chatPromptArgs = messager.onChatPrompt.getCall(0).args[0];
            assert.equal(chatPromptArgs.prompt.prompt, 'Generate a Memory Bank for this project');
            assert.equal(chatPromptArgs.prompt.escapedPrompt, 'Generate a Memory Bank for this project');
            assert.equal(chatPromptArgs.tabId, 'new-tab-id');
        });
        it('calls messager when regular rule is clicked', () => {
            const ruleItem = {
                id: 'test-rule-id',
                description: 'Test Rule',
            };
            onItemClick(ruleItem);
            sinon.assert.calledOnce(messager.onRuleClick);
            const callArgs = messager.onRuleClick.getCall(0).args[0];
            assert.equal(callArgs.tabId, 'test-tab-id');
            assert.equal(callArgs.type, 'rule');
            assert.equal(callArgs.id, 'test-rule-id');
        });
        it('does nothing when item has no id', () => {
            const itemWithoutId = {
                description: 'Item without ID',
            };
            onItemClick(itemWithoutId);
            sinon.assert.notCalled(messager.onRuleClick);
            sinon.assert.notCalled(showCustomFormStub);
        });
    });
    describe('folder click handling', () => {
        it('calls messager when folder is clicked', () => {
            const mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            rulesList.showLoading('test-tab-id');
            const onGroupClick = openTopBarButtonOverlayStub.getCall(0).args[0].events.onGroupClick;
            onGroupClick('test-folder');
            sinon.assert.calledOnce(messager.onRuleClick);
            const callArgs = messager.onRuleClick.getCall(0).args[0];
            assert.equal(callArgs.tabId, 'test-tab-id');
            assert.equal(callArgs.type, 'folder');
            assert.equal(callArgs.id, 'test-folder');
        });
    });
    describe('keyboard handling', () => {
        it('closes overlay when Escape is pressed', () => {
            const mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            rulesList.showLoading('test-tab-id');
            const onKeyPress = openTopBarButtonOverlayStub.getCall(0).args[0].events.onKeyPress;
            const escapeEvent = { key: 'Escape' };
            onKeyPress(escapeEvent);
            sinon.assert.calledOnce(mockOverlay.close);
        });
        it('does nothing when other keys are pressed', () => {
            const mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            rulesList.showLoading('test-tab-id');
            const onKeyPress = openTopBarButtonOverlayStub.getCall(0).args[0].events.onKeyPress;
            const enterEvent = { key: 'Enter' };
            onKeyPress(enterEvent);
            sinon.assert.notCalled(mockOverlay.close);
        });
    });
    describe('close', () => {
        it('closes the overlay', () => {
            const mockOverlay = {
                update: sinon.stub(),
                close: sinon.stub(),
            };
            openTopBarButtonOverlayStub.returns(mockOverlay);
            rulesList.showLoading('test-tab-id');
            rulesList.close();
            sinon.assert.calledOnce(mockOverlay.close);
        });
    });
    describe('convertRulesListToDetailedListGroup', () => {
        it('converts rules folder to detailed list group', () => {
            const rulesFolder = [
                {
                    folderName: 'test-folder',
                    active: true,
                    rules: [
                        {
                            id: 'rule-1',
                            name: 'Test Rule 1',
                            active: true,
                        },
                        {
                            id: 'rule-2',
                            name: 'Test Rule 2',
                            active: false,
                        },
                    ],
                },
                {
                    folderName: 'inactive-folder',
                    active: 'indeterminate',
                    rules: [],
                },
            ];
            const result = (0, rules_1.convertRulesListToDetailedListGroup)(rulesFolder);
            assert.equal(result.length, 3); // 2 folders + actions group
            assert.equal(result[0].groupName, 'test-folder');
            assert.equal(result[0].children?.length, 2);
            assert.equal(result[0].children?.[0].id, 'rule-1');
            assert.equal(result[0].children?.[0].description, 'Test Rule 1');
            assert.equal(result[1].groupName, 'inactive-folder');
            assert.equal(result[1].children?.length, 0);
            assert.equal(result[2].groupName, 'Actions');
            assert.equal(result[2].children?.length, 2); // Memory Bank + Create Rule
            assert.equal(result[2].children?.[0].id, rules_1.ContextRule.CreateMemoryBankId);
            assert.equal(result[2].children?.[1].id, rules_1.ContextRule.CreateRuleId);
        });
        it('handles empty rules array', () => {
            const result = (0, rules_1.convertRulesListToDetailedListGroup)([]);
            assert.equal(result.length, 1); // Only actions group
            assert.equal(result[0].groupName, 'Actions');
            assert.equal(result[0].children?.length, 2); // Memory Bank + Create Rule
            assert.equal(result[0].children?.[0].id, rules_1.ContextRule.CreateMemoryBankId);
            assert.equal(result[0].children?.[1].id, rules_1.ContextRule.CreateRuleId);
        });
    });
});
//# sourceMappingURL=rules.test.js.map