"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RulesList = exports.ContextRule = void 0;
exports.convertRulesListToDetailedListGroup = convertRulesListToDetailedListGroup;
const mynah_ui_1 = require("@aws/mynah-ui");
exports.ContextRule = {
    CreateRuleId: 'create-rule',
    CancelButtonId: 'cancel-create-rule',
    SubmitButtonId: 'submit-create-rule',
    RuleNameFieldId: 'rule-name',
};
class RulesList {
    mynahUi;
    messager;
    rulesList;
    tabId = '';
    constructor(mynahUi, messager) {
        this.mynahUi = mynahUi;
        this.messager = messager;
    }
    onRuleFolderClick = (groupName) => {
        this.messager.onRuleClick({ tabId: this.tabId, type: 'folder', id: groupName });
    };
    onRuleClick = (item) => {
        if (item.id) {
            if (item.id === exports.ContextRule.CreateRuleId) {
                this.rulesList?.close();
                this.mynahUi.showCustomForm(this.tabId, [
                    {
                        id: exports.ContextRule.RuleNameFieldId,
                        type: 'textinput',
                        mandatory: true,
                        autoFocus: true,
                        title: 'Rule name',
                        placeholder: 'Enter rule name',
                        validationPatterns: {
                            patterns: [
                                {
                                    pattern: /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,99}$/,
                                    errorMessage: 'Use only letters, numbers, hyphens, and underscores, starting with a letter or number. Maximum 100 characters.',
                                },
                            ],
                        },
                        description: "This will create a [rule name].md file in your project's .amazonq/rules folder.",
                    },
                ], [
                    {
                        id: exports.ContextRule.CancelButtonId,
                        text: 'Cancel',
                        status: 'clear',
                        waitMandatoryFormItems: false,
                    },
                    {
                        id: exports.ContextRule.SubmitButtonId,
                        text: 'Create',
                        status: 'main',
                        waitMandatoryFormItems: true,
                    },
                ], `Create a rule`);
            }
            else {
                this.messager.onRuleClick({ tabId: this.tabId, type: 'rule', id: item.id });
            }
        }
    };
    showLoading(tabId) {
        this.tabId = tabId;
        const rulesList = this.mynahUi.openTopBarButtonOverlay({
            tabId: this.tabId,
            topBarButtonOverlay: {
                list: [{ groupName: 'Loading rules...' }],
                selectable: false,
            },
            events: {
                onGroupClick: this.onRuleFolderClick,
                onItemClick: this.onRuleClick,
                onClose: this.onClose,
                onKeyPress: this.onKeyPress,
            },
        });
        this.rulesList = {
            ...rulesList,
            changeTarget: () => { },
            getTargetElementId: () => {
                return undefined;
            },
        };
    }
    show(params) {
        this.tabId = params.tabId;
        if (this.rulesList) {
            this.rulesList.update({
                filterOptions: params.filterOptions?.map(option => ({
                    ...option,
                    icon: option.icon,
                })),
                list: convertRulesListToDetailedListGroup(params.rules),
                selectable: 'clickable',
            });
        }
        else {
            const rulesList = this.mynahUi.openTopBarButtonOverlay({
                tabId: this.tabId,
                topBarButtonOverlay: {
                    list: convertRulesListToDetailedListGroup(params.rules),
                    selectable: 'clickable',
                },
                events: {
                    onGroupClick: this.onRuleFolderClick,
                    onItemClick: this.onRuleClick,
                    onClose: this.onClose,
                    onKeyPress: this.onKeyPress,
                },
            });
            this.rulesList = {
                ...rulesList,
                changeTarget: () => { },
                getTargetElementId: () => {
                    return undefined;
                },
            };
        }
    }
    onKeyPress = (e) => {
        if (e.key === 'Escape') {
            this.close();
        }
    };
    close() {
        this.rulesList?.close();
    }
    onClose = () => {
        this.rulesList = undefined;
    };
}
exports.RulesList = RulesList;
const createRuleListItem = {
    description: 'Create a new rule',
    icon: mynah_ui_1.MynahIcons.LIST_ADD,
    id: exports.ContextRule.CreateRuleId,
};
function convertRulesListToDetailedListGroup(rules) {
    return rules
        .map(ruleFolder => ({
        groupName: ruleFolder.folderName,
        actions: [
            {
                id: ruleFolder.folderName,
                icon: convertRuleStatusToIcon(ruleFolder.active),
                status: 'clear',
            },
        ],
        icon: mynah_ui_1.MynahIcons.FOLDER,
        childrenIndented: true,
        children: ruleFolder.rules.map(rule => ({
            id: rule.id,
            icon: mynah_ui_1.MynahIcons.CHECK_LIST,
            description: rule.name,
            actions: [{ id: rule.id, icon: convertRuleStatusToIcon(rule.active), status: 'clear' }],
        })),
    }))
        .concat({ children: [createRuleListItem] });
}
function convertRuleStatusToIcon(status) {
    if (status === true) {
        return mynah_ui_1.MynahIcons.OK;
    }
    else if (status === 'indeterminate') {
        return mynah_ui_1.MynahIcons.MINUS;
    }
    return undefined;
}
//# sourceMappingURL=rules.js.map