"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mocha_1 = require("mocha");
const ts_sinon_1 = require("ts-sinon");
const assert = require('assert');
const withAdapter_1 = require("./withAdapter");
const mynah_ui_1 = require("@aws/mynah-ui");
const disclaimer_1 = require("./texts/disclaimer");
(0, mocha_1.describe)('withAdapter', () => {
    let defaultEventHandlers;
    let mynahUIRef;
    let chatClientAdapter;
    let customEventHandlers;
    let mynahUiPropsWithAdapter;
    let tabFactory;
    beforeEach(() => {
        // Set up base MynahUIProps with stub methods
        defaultEventHandlers = {
            onTabAdd: ts_sinon_1.default.stub(),
            onTabChange: ts_sinon_1.default.stub(),
            onBeforeTabRemove: ts_sinon_1.default.stub().returns(true),
            onTabRemove: ts_sinon_1.default.stub(),
            onChatPrompt: ts_sinon_1.default.stub(),
            onStopChatResponse: ts_sinon_1.default.stub(),
            onLinkClick: ts_sinon_1.default.stub(),
            onSourceLinkClick: ts_sinon_1.default.stub(),
            onInfoLinkClick: ts_sinon_1.default.stub(),
            onCodeInsertToCursorPosition: ts_sinon_1.default.stub(),
            onCopyCodeToClipboard: ts_sinon_1.default.stub(),
            onCodeBlockActionClicked: ts_sinon_1.default.stub(),
            onFileClick: ts_sinon_1.default.stub(),
            onFileActionClick: ts_sinon_1.default.stub(),
            onVote: ts_sinon_1.default.stub(),
            onSendFeedback: ts_sinon_1.default.stub(),
            onFollowUpClicked: ts_sinon_1.default.stub(),
            onInBodyButtonClicked: ts_sinon_1.default.stub(),
            onCustomFormAction: ts_sinon_1.default.stub(),
            onFormTextualItemKeyPress: ts_sinon_1.default.stub().returns(false),
            onQuickCommandGroupActionClick: ts_sinon_1.default.stub(),
            onContextSelected: ts_sinon_1.default.stub().returns(true),
            onChatItemEngagement: ts_sinon_1.default.stub(),
            onShowMoreWebResultsClick: ts_sinon_1.default.stub(),
            onChatPromptProgressActionButtonClicked: ts_sinon_1.default.stub(),
            onTabbedContentTabChange: ts_sinon_1.default.stub(),
            onFormLinkClick: ts_sinon_1.default.stub(),
            onFormModifierEnterPress: ts_sinon_1.default.stub(),
            onTabBarButtonClick: ts_sinon_1.default.stub(),
            onReady: ts_sinon_1.default.stub(),
            onResetStore: ts_sinon_1.default.stub(),
            onFocusStateChanged: ts_sinon_1.default.stub(),
        };
        // Set up MynahUI reference
        mynahUIRef = {
            mynahUI: {
                getSelectedTabId: ts_sinon_1.default.stub().returns('tab-1'),
            },
        };
        // Set up custom event handler with stub methods
        customEventHandlers = {
            onTabAdd: ts_sinon_1.default.stub(),
            onTabChange: ts_sinon_1.default.stub(),
            onBeforeTabRemove: ts_sinon_1.default.stub().returns(true),
            onTabRemove: ts_sinon_1.default.stub(),
            onChatPrompt: ts_sinon_1.default.stub(),
            onStopChatResponse: ts_sinon_1.default.stub(),
            onLinkClick: ts_sinon_1.default.stub(),
            onSourceLinkClick: ts_sinon_1.default.stub(),
            onInfoLinkClick: ts_sinon_1.default.stub(),
            onCodeInsertToCursorPosition: ts_sinon_1.default.stub(),
            onCopyCodeToClipboard: ts_sinon_1.default.stub(),
            onCodeBlockActionClicked: ts_sinon_1.default.stub(),
            onFileClick: ts_sinon_1.default.stub(),
            onFileActionClick: ts_sinon_1.default.stub(),
            onVote: ts_sinon_1.default.stub(),
            onSendFeedback: ts_sinon_1.default.stub(),
            onFollowUpClicked: ts_sinon_1.default.stub(),
            onInBodyButtonClicked: ts_sinon_1.default.stub(),
            onCustomFormAction: ts_sinon_1.default.stub(),
            onFormTextualItemKeyPress: ts_sinon_1.default.stub().returns(false),
            onQuickCommandGroupActionClick: ts_sinon_1.default.stub(),
            onContextSelected: ts_sinon_1.default.stub().returns(true),
            onChatItemEngagement: ts_sinon_1.default.stub(),
            onShowMoreWebResultsClick: ts_sinon_1.default.stub(),
            onChatPromptProgressActionButtonClicked: ts_sinon_1.default.stub(),
            onTabbedContentTabChange: ts_sinon_1.default.stub(),
            onFormLinkClick: ts_sinon_1.default.stub(),
            onFormModifierEnterPress: ts_sinon_1.default.stub(),
            onTabBarButtonClick: ts_sinon_1.default.stub(),
            onReady: ts_sinon_1.default.stub(),
            onResetStore: ts_sinon_1.default.stub(),
            onFocusStateChanged: ts_sinon_1.default.stub(),
        };
        // Set up chat client adapter
        chatClientAdapter = {
            createChatEventHandler: ts_sinon_1.default.stub().returns(customEventHandlers),
            isSupportedTab: ts_sinon_1.default.stub().callsFake(tabId => tabId === 'supported-tab'),
            isSupportedQuickAction: ts_sinon_1.default.stub().callsFake(command => command === '/supported-command'),
            handleMessageReceive: ts_sinon_1.default.stub(),
            handleQuickAction: ts_sinon_1.default.stub(),
        };
        // Set up tab factory
        tabFactory = {
            isRerouteEnabled: ts_sinon_1.default.stub().returns(false),
            isCodeReviewInChatEnabled: ts_sinon_1.default.stub().returns(false),
        };
        // Create the enhanced props
        mynahUiPropsWithAdapter = (0, withAdapter_1.withAdapter)(defaultEventHandlers, mynahUIRef, chatClientAdapter, tabFactory);
    });
    (0, mocha_1.afterEach)(() => {
        ts_sinon_1.default.restore();
    });
    const testEventHandler = ({ handlerName, args, supportedTabId = 'supported-tab', unsupportedTabId = 'unsupported-tab', }) => {
        (0, mocha_1.describe)(handlerName, () => {
            (0, mocha_1.it)(`should delegate ${handlerName} to custom handler for supported tabs`, () => {
                const handlerArgs = args.map(arg => (arg === '__TAB_ID__' ? supportedTabId : arg));
                mynahUiPropsWithAdapter[handlerName]?.(...handlerArgs);
                ts_sinon_1.default.assert.calledOnceWithExactly(
                // @ts-ignore
                customEventHandlers[handlerName], ...handlerArgs);
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers[handlerName]);
            });
            (0, mocha_1.it)(`should delegate ${handlerName} to original handler for unsupported tabs`, () => {
                const handlerArgs = args.map(arg => (arg === '__TAB_ID__' ? unsupportedTabId : arg));
                mynahUiPropsWithAdapter[handlerName]?.(...handlerArgs);
                // @ts-ignore
                ts_sinon_1.default.assert.notCalled(customEventHandlers[handlerName]);
                ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers[handlerName], ...handlerArgs);
            });
        });
    };
    (0, mocha_1.it)('should throw error if custom event handler is not defined', () => {
        const invalidAdapter = {
            createChatEventHandler: ts_sinon_1.default.stub().returns(undefined),
            isSupportedTab: ts_sinon_1.default.stub(),
            isSupportedQuickAction: ts_sinon_1.default.stub(),
            handleMessageReceive: ts_sinon_1.default.stub(),
            handleQuickAction: ts_sinon_1.default.stub(),
        };
        assert.throws(() => {
            (0, withAdapter_1.withAdapter)(defaultEventHandlers, mynahUIRef, invalidAdapter, tabFactory);
        }, new Error('Custom ChatEventHandler is not defined'));
    });
    (0, mocha_1.describe)('Standard Events routing on tabId as first argument', () => {
        testEventHandler({
            handlerName: 'onTabAdd',
            args: ['__TAB_ID__'],
        });
        testEventHandler({
            handlerName: 'onTabChange',
            args: ['__TAB_ID__'],
        });
        testEventHandler({
            handlerName: 'onTabRemove',
            args: ['__TAB_ID__'],
        });
        testEventHandler({
            handlerName: 'onStopChatResponse',
            args: ['__TAB_ID__'],
        });
        const mouseEvent = {
            preventDefault: ts_sinon_1.default.stub(),
            stopPropagation: ts_sinon_1.default.stub(),
            stopImmediatePropagation: ts_sinon_1.default.stub(),
        };
        testEventHandler({
            handlerName: 'onLinkClick',
            args: ['__TAB_ID__', 'message-1', 'https://example.com', mouseEvent, 'event-1'],
        });
        testEventHandler({
            handlerName: 'onSourceLinkClick',
            args: ['__TAB_ID__', 'message-1', 'https://example.com', mouseEvent, 'event-1'],
        });
        testEventHandler({
            handlerName: 'onInfoLinkClick',
            args: ['__TAB_ID__', 'https://example.com', mouseEvent, 'event-1'],
        });
        testEventHandler({
            handlerName: 'onCodeInsertToCursorPosition',
            args: [
                '__TAB_ID__',
                'message-1',
                'console.log("Hello")',
                'selection',
                [{ information: 'ref1' }],
                'event-1',
                1,
                2,
            ],
        });
        testEventHandler({
            handlerName: 'onCopyCodeToClipboard',
            args: [
                '__TAB_ID__',
                'message-1',
                'console.log("Hello")',
                'selection',
                [{ information: 'ref1' }],
                'event-1',
                1,
                2,
            ],
        });
        testEventHandler({
            handlerName: 'onCodeBlockActionClicked',
            args: [
                '__TAB_ID__',
                'message-1',
                'action-1',
                'data',
                'console.log("Hello")',
                'selection',
                [{ information: 'ref1' }],
                'event-1',
                1,
                2,
            ],
        });
        testEventHandler({
            handlerName: 'onFileClick',
            args: ['__TAB_ID__', '/path/to/file.js', false, 'message-1', 'event-1'],
        });
        testEventHandler({
            handlerName: 'onFileActionClick',
            args: ['__TAB_ID__', 'message-1', '/path/to/file.js', 'open', 'event-1'],
        });
        testEventHandler({
            handlerName: 'onVote',
            args: ['__TAB_ID__', 'message-1', mynah_ui_1.RelevancyVoteType.UP, 'event-1'],
        });
        testEventHandler({
            handlerName: 'onSendFeedback',
            args: [
                '__TAB_ID__',
                {
                    messageId: 'feedback',
                    tabId: 'tab-id',
                    selectedOption: '1',
                    comment: 'Great service!',
                },
                'event-1',
            ],
        });
        testEventHandler({
            handlerName: 'onFollowUpClicked',
            args: ['__TAB_ID__', 'message-1', { pillText: 'Tell me more' }, 'event-1'],
        });
        testEventHandler({
            handlerName: 'onCustomFormAction',
            args: ['__TAB_ID__', { id: 'action-id' }, 'event-1'],
        });
        testEventHandler({
            handlerName: 'onQuickCommandGroupActionClick',
            args: ['__TAB_ID__', { id: 'action-id' }, 'event-1'],
        });
        testEventHandler({
            handlerName: 'onChatItemEngagement',
            args: [
                '__TAB_ID__',
                'message-1',
                {
                    onChatItemEngagement: 'interaction',
                    engagementDurationTillTrigger: 1,
                    totalMouseDistanceTraveled: { x: 1, y: 2 },
                },
                'event-1',
            ],
        });
        testEventHandler({
            handlerName: 'onShowMoreWebResultsClick',
            args: ['__TAB_ID__', 'message-1', 'event-1'],
        });
        testEventHandler({
            handlerName: 'onChatPromptProgressActionButtonClicked',
            args: [
                '__TAB_ID__',
                {
                    id: 'action-id',
                },
                'event-1',
            ],
        });
        testEventHandler({
            handlerName: 'onTabbedContentTabChange',
            args: ['__TAB_ID__', 'message-1', 'content-tab-1', 'event-1'],
        });
    });
    (0, mocha_1.describe)('Special Routing Logic', () => {
        (0, mocha_1.it)('should delegate onBeforeTabRemove to custom handler for supported tabs', () => {
            const result = mynahUiPropsWithAdapter.onBeforeTabRemove?.('supported-tab', 'event-1');
            ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onBeforeTabRemove, 'supported-tab', 'event-1');
            ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onBeforeTabRemove);
            assert.equal(result, true);
        });
        (0, mocha_1.describe)('onChatPrompt', () => {
            (0, mocha_1.it)('should delegate onChatPrompt to custom handler for supported tabs', () => {
                const prompt = { prompt: 'Hello world', escapedPrompt: 'Hello world' };
                mynahUiPropsWithAdapter.onChatPrompt?.('supported-tab', prompt, 'event-1');
                ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onChatPrompt, 'supported-tab', prompt, 'event-1');
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onChatPrompt);
            });
            (0, mocha_1.it)('should delegate quick action commands to adapter handler', () => {
                const prompt = { prompt: 'Test command', escapedPrompt: 'Test command', command: '/supported-command' };
                mynahUiPropsWithAdapter.onChatPrompt?.('unsupported-tab', prompt, 'event-1');
                ts_sinon_1.default.assert.notCalled(customEventHandlers.onChatPrompt);
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onChatPrompt);
                ts_sinon_1.default.assert.calledOnceWithExactly(chatClientAdapter.handleQuickAction, prompt, 'unsupported-tab', 'event-1');
            });
            (0, mocha_1.it)('should delegate onChatPrompt to original handler for unsupported tabs and commands', () => {
                const prompt = { prompt: 'Hello world', escapedPrompt: 'Hello world' };
                mynahUiPropsWithAdapter.onChatPrompt?.('unsupported-tab', prompt, 'event-1');
                ts_sinon_1.default.assert.notCalled(customEventHandlers.onChatPrompt);
                ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onChatPrompt, 'unsupported-tab', prompt, 'event-1');
            });
        });
        (0, mocha_1.describe)('onInBodyButtonClicked', () => {
            (0, mocha_1.it)('should delegate onInBodyButtonClicked to original handler for disclaimerAcknowledge action', () => {
                const action = { id: disclaimer_1.disclaimerAcknowledgeButtonId, text: 'Click me' };
                mynahUiPropsWithAdapter.onInBodyButtonClicked?.('supported-tab', 'message-1', action, 'event-1');
                ts_sinon_1.default.assert.notCalled(customEventHandlers.onInBodyButtonClicked);
                ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onInBodyButtonClicked, 'supported-tab', 'message-1', action, 'event-1');
            });
            (0, mocha_1.it)('should delegate onInBodyButtonClicked to custom handler for supported tabs', () => {
                const action = { id: 'button-1', text: 'Click me' };
                mynahUiPropsWithAdapter.onInBodyButtonClicked?.('supported-tab', 'message-1', action, 'event-1');
                ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onInBodyButtonClicked, 'supported-tab', 'message-1', action, 'event-1');
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onInBodyButtonClicked);
            });
            (0, mocha_1.it)('should delegate onInBodyButtonClicked to original handler for unsupported tab', () => {
                const action = { id: 'button-1', text: 'Click me' };
                mynahUiPropsWithAdapter.onInBodyButtonClicked?.('unsupported-tab', 'message-1', action, 'event-1');
                ts_sinon_1.default.assert.notCalled(customEventHandlers.onInBodyButtonClicked);
                ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onInBodyButtonClicked, 'unsupported-tab', 'message-1', action, 'event-1');
            });
        });
        (0, mocha_1.describe)('onFormTextualItemKeyPress', () => {
            (0, mocha_1.it)('should delegate onFormTextualItemKeyPress to custom handler for supported tabs', () => {
                const event = { key: 'Enter' };
                const formData = { input: 'test' };
                mynahUiPropsWithAdapter.onFormTextualItemKeyPress?.(event, formData, 'item-1', 'supported-tab', 'event-1');
                ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onFormTextualItemKeyPress, event, formData, 'item-1', 'supported-tab', 'event-1');
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onFormTextualItemKeyPress);
            });
            (0, mocha_1.it)('should delegate onFormTextualItemKeyPress to original handler for unsupported tabs', () => {
                const event = { key: 'Enter' };
                const formData = { input: 'test' };
                mynahUiPropsWithAdapter.onFormTextualItemKeyPress?.(event, formData, 'item-1', 'unsupported-tab', 'event-1');
                ts_sinon_1.default.assert.notCalled(customEventHandlers.onFormTextualItemKeyPress);
                ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onFormTextualItemKeyPress, event, formData, 'item-1', 'unsupported-tab', 'event-1');
            });
        });
        (0, mocha_1.describe)('onFormModifierEnterPress', () => {
            (0, mocha_1.it)('should delegate onFormModifierEnterPress to custom handler for supported tabs', () => {
                const formData = { input: 'test' };
                mynahUiPropsWithAdapter.onFormModifierEnterPress?.(formData, 'supported-tab', 'event-1');
                ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onFormModifierEnterPress, formData, 'supported-tab', 'event-1');
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onFormModifierEnterPress);
            });
            (0, mocha_1.it)('should delegate onFormModifierEnterPress to original handler for unsupported tabs', () => {
                const formData = { input: 'test' };
                mynahUiPropsWithAdapter.onFormModifierEnterPress?.(formData, 'unsupported-tab', 'event-1');
                ts_sinon_1.default.assert.notCalled(customEventHandlers.onFormModifierEnterPress);
                ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onFormModifierEnterPress, formData, 'unsupported-tab', 'event-1');
            });
        });
        (0, mocha_1.describe)('onContextSelected', () => {
            (0, mocha_1.it)('should delegate onContextSelected to custom handler for supported tabs', () => {
                const contextItem = { command: 'command-1' };
                mynahUiPropsWithAdapter.onContextSelected?.(contextItem, 'supported-tab', 'event-1');
                ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onContextSelected, contextItem, 'supported-tab', 'event-1');
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onContextSelected);
            });
            (0, mocha_1.it)('should delegate onContextSelected to original handler for unsupported tabs', () => {
                const contextItem = { command: 'command-1' };
                mynahUiPropsWithAdapter.onContextSelected?.(contextItem, 'unsupported-tab', 'event-1');
                ts_sinon_1.default.assert.notCalled(customEventHandlers.onContextSelected);
                ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onContextSelected, contextItem, 'unsupported-tab', 'event-1');
            });
        });
        (0, mocha_1.describe)('onFormLinkClick', () => {
            const mouseEvent = { preventDefault: ts_sinon_1.default.stub() };
            (0, mocha_1.it)('should delegate onFormLinkClick to custom handler if it exists', () => {
                mynahUiPropsWithAdapter.onFormLinkClick?.('https://example.com', mouseEvent, 'event-1');
                ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onFormLinkClick, 'https://example.com', mouseEvent, 'event-1');
                ts_sinon_1.default.assert.notCalled(defaultEventHandlers.onFormLinkClick);
            });
            (0, mocha_1.it)('should delegate onFormLinkClick to original handler if adapter handler does not exist', () => {
                const onFormLinkClickStub = ts_sinon_1.default.stub();
                const mynahUiPropsWithAdapter = (0, withAdapter_1.withAdapter)({
                    onFormLinkClick: onFormLinkClickStub,
                }, mynahUIRef, 
                // @ts-ignore
                {
                    createChatEventHandler: () => ({}),
                }, tabFactory);
                const customOnFormLinkClickHandler = customEventHandlers.onFileActionClick;
                customEventHandlers.onFileActionClick = undefined;
                mynahUiPropsWithAdapter.onFormLinkClick?.('https://example.com', mouseEvent, 'event-1');
                ts_sinon_1.default.assert.notCalled(customOnFormLinkClickHandler);
                ts_sinon_1.default.assert.calledOnceWithExactly(onFormLinkClickStub, 'https://example.com', mouseEvent, 'event-1');
            });
        });
        (0, mocha_1.it)('should call route onTabBarButtonClick only to default handler', () => {
            mynahUiPropsWithAdapter.onTabBarButtonClick?.('tab-1', 'button-1');
            ts_sinon_1.default.assert.notCalled(customEventHandlers.onTabBarButtonClick);
            ts_sinon_1.default.assert.calledOnceWithMatch(defaultEventHandlers.onTabBarButtonClick, 'tab-1', 'button-1');
        });
        (0, mocha_1.it)('should call both custom and original onReady handlers', () => {
            mynahUiPropsWithAdapter.onReady?.();
            ts_sinon_1.default.assert.calledOnce(customEventHandlers.onReady);
            ts_sinon_1.default.assert.calledOnce(defaultEventHandlers.onReady);
        });
        (0, mocha_1.it)('should call both custom and original onResetStore handlers', () => {
            mynahUiPropsWithAdapter.onResetStore?.('tab-1');
            ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onResetStore, 'tab-1');
            ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onResetStore, 'tab-1');
        });
        (0, mocha_1.it)('should call both custom and original onFocusStateChanged handlers', () => {
            ;
            (mynahUIRef.mynahUI?.getSelectedTabId).returns('supported-tab');
            mynahUiPropsWithAdapter.onFocusStateChanged?.(true);
            ts_sinon_1.default.assert.calledOnceWithExactly(customEventHandlers.onFocusStateChanged, true);
            ts_sinon_1.default.assert.calledOnceWithExactly(defaultEventHandlers.onFocusStateChanged, true);
        });
    });
});
//# sourceMappingURL=withAdapter.test.js.map