"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatHistoryList = exports.ChatHistory = void 0;
const utils_1 = require("../utils");
exports.ChatHistory = {
    TabBarButtonId: 'history_sheet',
};
class ChatHistoryList {
    mynahUi;
    messager;
    historyDetailedList;
    constructor(mynahUi, messager) {
        this.mynahUi = mynahUi;
        this.messager = messager;
    }
    show(params) {
        const detailedList = {
            header: params.header,
            filterOptions: params.filterOptions?.map(filter => ({
                ...filter,
                icon: (0, utils_1.toMynahIcon)(filter.icon),
            })),
            list: this.toConversationGroups(params.list),
        };
        // set auto focus on the 1st filter option item
        if (detailedList.filterOptions && detailedList.filterOptions.length > 0) {
            // we currently support only text-based items
            ;
            detailedList.filterOptions[0].autoFocus = true;
        }
        if (this.historyDetailedList) {
            this.historyDetailedList.update(detailedList);
        }
        else {
            this.historyDetailedList = this.mynahUi.openDetailedList({
                detailedList: detailedList,
                events: {
                    onFilterValueChange: this.onFilterValueChange,
                    onKeyPress: this.onKeyPress,
                    onItemSelect: this.onItemSelect,
                    onActionClick: this.onActionClick,
                    onClose: this.onClose,
                },
            });
        }
    }
    close() {
        this.historyDetailedList?.close();
    }
    onFilterValueChange = (filterValues) => {
        this.messager.onListConversations(filterValues);
    };
    onItemSelect = (item) => {
        if (!item.id) {
            throw new Error('Conversation id is not defined');
        }
        if (item.id === 'empty') {
            return;
        }
        this.messager.onConversationClick(item.id);
    };
    onActionClick = (action) => {
        const conversationAction = this.getConversationAction(action.text);
        this.messager.onConversationClick(action.id, conversationAction);
    };
    onClose = () => {
        this.historyDetailedList = undefined;
    };
    onKeyPress = (e) => {
        if (e.key === 'Escape') {
            this.close();
        }
        else if (e.key === 'Enter') {
            const targetElementId = this.historyDetailedList?.getTargetElementId();
            if (targetElementId) {
                this.onItemSelect({
                    id: targetElementId,
                });
            }
        }
        else if (e.key === 'ArrowUp') {
            this.historyDetailedList?.changeTarget('up');
        }
        else if (e.key === 'ArrowDown') {
            this.historyDetailedList?.changeTarget('down');
        }
    };
    toConversationGroups = (groups) => {
        return groups.map(group => ({
            groupName: group.groupName,
            icon: (0, utils_1.toMynahIcon)(group.icon),
            children: group.items?.map(item => ({
                ...item,
                icon: (0, utils_1.toMynahIcon)(item.icon),
                actions: item.actions?.map(action => ({
                    ...action,
                    icon: (0, utils_1.toMynahIcon)(action.icon),
                })),
            })),
        }));
    };
    getConversationAction = (actionText) => {
        switch (actionText) {
            case 'Export':
                return 'export';
            case 'Delete':
                return 'delete';
            default:
                throw new Error(`Unsupported action: ${actionText}`);
        }
    };
}
exports.ChatHistoryList = ChatHistoryList;
//# sourceMappingURL=history.js.map