"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const fs_1 = require("fs");
const electron_1 = require("electron");
const electron_util_1 = require("electron-util");
const config_1 = require("./config");
const spell_checker_1 = require("./spell-checker");
const util_1 = require("./util");
const emoji_1 = require("./emoji");
const menu_bar_mode_1 = require("./menu-bar-mode");
const constants_1 = require("./constants");
async function updateMenu() {
    const newConversationItem = {
        label: 'New Conversation',
        accelerator: 'CommandOrControl+N',
        click() {
            util_1.sendAction('new-conversation');
        }
    };
    const switchItems = [
        {
            label: 'Switch to Work Chat…',
            accelerator: 'CommandOrControl+Shift+2',
            visible: !config_1.default.get('useWorkChat'),
            click() {
                config_1.default.set('useWorkChat', true);
                electron_1.app.relaunch();
                electron_1.app.quit();
            }
        },
        {
            label: 'Switch to Messenger…',
            accelerator: 'CommandOrControl+Shift+1',
            visible: config_1.default.get('useWorkChat'),
            click() {
                config_1.default.set('useWorkChat', false);
                electron_1.app.relaunch();
                electron_1.app.quit();
            }
        },
        {
            label: 'Log Out',
            click() {
                util_1.sendAction('log-out');
            }
        }
    ];
    const vibrancySubmenu = [
        {
            label: 'No Vibrancy',
            type: 'checkbox',
            checked: config_1.default.get('vibrancy') === 'none',
            async click() {
                config_1.default.set('vibrancy', 'none');
                util_1.sendAction('update-vibrancy');
                await updateMenu();
            }
        },
        {
            label: 'Sidebar-only Vibrancy',
            type: 'checkbox',
            checked: config_1.default.get('vibrancy') === 'sidebar',
            async click() {
                config_1.default.set('vibrancy', 'sidebar');
                util_1.sendAction('update-vibrancy');
                await updateMenu();
            }
        },
        {
            label: 'Full-window Vibrancy',
            type: 'checkbox',
            checked: config_1.default.get('vibrancy') === 'full',
            async click() {
                config_1.default.set('vibrancy', 'full');
                util_1.sendAction('update-vibrancy');
                await updateMenu();
            }
        }
    ];
    const sidebarSubmenu = [
        {
            label: 'Adaptive Sidebar',
            type: 'checkbox',
            checked: config_1.default.get('sidebar') === 'default',
            async click() {
                config_1.default.set('sidebar', 'default');
                util_1.sendAction('update-sidebar');
                await updateMenu();
            }
        },
        {
            label: 'Hide Sidebar',
            type: 'checkbox',
            checked: config_1.default.get('sidebar') === 'hidden',
            accelerator: 'CommandOrControl+Shift+S',
            async click() {
                // Toggle between default and hidden
                config_1.default.set('sidebar', config_1.default.get('sidebar') === 'hidden' ? 'default' : 'hidden');
                util_1.sendAction('update-sidebar');
                await updateMenu();
            }
        },
        {
            label: 'Narrow Sidebar',
            type: 'checkbox',
            checked: config_1.default.get('sidebar') === 'narrow',
            async click() {
                config_1.default.set('sidebar', 'narrow');
                util_1.sendAction('update-sidebar');
                await updateMenu();
            }
        },
        {
            label: 'Wide Sidebar',
            type: 'checkbox',
            checked: config_1.default.get('sidebar') === 'wide',
            async click() {
                config_1.default.set('sidebar', 'wide');
                util_1.sendAction('update-sidebar');
                await updateMenu();
            }
        }
    ];
    const privacySubmenu = [
        {
            label: 'Block Seen Indicator',
            type: 'checkbox',
            checked: config_1.default.get('block.chatSeen'),
            click(menuItem) {
                config_1.default.set('block.chatSeen', menuItem.checked);
            }
        },
        {
            label: 'Block Typing Indicator',
            type: 'checkbox',
            checked: config_1.default.get('block.typingIndicator'),
            click(menuItem) {
                config_1.default.set('block.typingIndicator', menuItem.checked);
            }
        },
        {
            label: 'Block Delivery Receipts',
            type: 'checkbox',
            checked: config_1.default.get('block.deliveryReceipt'),
            click(menuItem) {
                config_1.default.set('block.deliveryReceipt', menuItem.checked);
            }
        }
    ];
    const advancedSubmenu = [
        {
            label: 'Custom Styles',
            click() {
                const filePath = path.join(electron_1.app.getPath('userData'), 'custom.css');
                const defaultCustomStyle = `/*
This is the custom styles file where you can add anything you want.
The styles here will be injected into Caprine and will override default styles.
If you want to disable styles but keep the config, just comment the lines that you don't want to be used.

Here are some dark mode color variables to get you started.
Edit them to change color scheme of Caprine.
Press Command/Ctrl+R in Caprine to see your changes.
*/

:root {
	--base: #000;
	--base-ninety: rgba(255, 255, 255, 0.9);
	--base-seventy-five: rgba(255, 255, 255, 0.75);
	--base-seventy: rgba(255, 255, 255, 0.7);
	--base-fifty: rgba(255, 255, 255, 0.5);
	--base-fourty: rgba(255, 255, 255, 0.4);
	--base-thirty: rgba(255, 255, 255, 0.3);
	--base-twenty: rgba(255, 255, 255, 0.2);
	--base-five: rgba(255, 255, 255, 0.05);
	--base-ten: rgba(255, 255, 255, 0.1);
	--base-nine: rgba(255, 255, 255, 0.09);
	--container-color: #323232;
	--container-dark-color: #1e1e1e;
	--list-header-color: #222;
	--blue: #0084ff;
	--selected-conversation-background: linear-gradient(hsla(209, 110%, 45%, 0.9), hsla(209, 110%, 42%, 0.9));
}
`;
                if (!fs_1.existsSync(filePath)) {
                    fs_1.writeFileSync(filePath, defaultCustomStyle, 'utf8');
                }
                electron_1.shell.openPath(filePath);
            }
        }
    ];
    const preferencesSubmenu = [
        {
            label: 'Privacy',
            submenu: privacySubmenu
        },
        {
            label: 'Emoji Style',
            submenu: await emoji_1.generateSubmenu(updateMenu)
        },
        {
            label: 'Bounce Dock on Message',
            type: 'checkbox',
            visible: electron_util_1.is.macos,
            checked: config_1.default.get('bounceDockOnMessage'),
            click() {
                config_1.default.set('bounceDockOnMessage', !config_1.default.get('bounceDockOnMessage'));
            }
        },
        {
            label: 'Autoplay Videos',
            id: 'video-autoplay',
            type: 'checkbox',
            checked: config_1.default.get('autoplayVideos'),
            click() {
                config_1.default.set('autoplayVideos', !config_1.default.get('autoplayVideos'));
                util_1.sendAction('toggle-video-autoplay');
            }
        },
        {
            label: 'Show Message Preview in Notifications',
            type: 'checkbox',
            checked: config_1.default.get('notificationMessagePreview'),
            click(menuItem) {
                config_1.default.set('notificationMessagePreview', menuItem.checked);
            }
        },
        {
            label: 'Mute Notifications',
            id: 'mute-notifications',
            type: 'checkbox',
            checked: config_1.default.get('notificationsMuted'),
            click() {
                util_1.sendAction('toggle-mute-notifications');
            }
        },
        {
            label: 'Mute Call Ringtone',
            type: 'checkbox',
            checked: config_1.default.get('callRingtoneMuted'),
            click() {
                config_1.default.set('callRingtoneMuted', !config_1.default.get('callRingtoneMuted'));
            }
        },
        {
            label: 'Show Unread Badge',
            type: 'checkbox',
            checked: config_1.default.get('showUnreadBadge'),
            click() {
                config_1.default.set('showUnreadBadge', !config_1.default.get('showUnreadBadge'));
                util_1.sendAction('reload');
            }
        },
        {
            label: 'Spell Checker',
            type: 'checkbox',
            checked: config_1.default.get('isSpellCheckerEnabled'),
            click() {
                config_1.default.set('isSpellCheckerEnabled', !config_1.default.get('isSpellCheckerEnabled'));
                util_1.showRestartDialog('Caprine needs to be restarted to enable or disable the spell checker.');
            }
        },
        {
            label: 'Hardware Acceleration',
            type: 'checkbox',
            checked: config_1.default.get('hardwareAcceleration'),
            click() {
                config_1.default.set('hardwareAcceleration', !config_1.default.get('hardwareAcceleration'));
                util_1.showRestartDialog('Caprine needs to be restarted to change hardware acceleration.');
            }
        },
        {
            label: 'Show Menu Bar Icon',
            id: 'menuBarMode',
            type: 'checkbox',
            visible: electron_util_1.is.macos,
            checked: config_1.default.get('menuBarMode'),
            click() {
                config_1.default.set('menuBarMode', !config_1.default.get('menuBarMode'));
                menu_bar_mode_1.toggleMenuBarMode(util_1.getWindow());
            }
        },
        {
            label: 'Always on Top',
            id: 'always-on-top',
            type: 'checkbox',
            accelerator: 'CommandOrControl+Shift+T',
            checked: config_1.default.get('alwaysOnTop'),
            click(menuItem, focusedWindow) {
                config_1.default.set('alwaysOnTop', menuItem.checked);
                focusedWindow === null || focusedWindow === void 0 ? void 0 : focusedWindow.setAlwaysOnTop(menuItem.checked);
            }
        },
        {
            label: 'Launch at Login',
            visible: electron_util_1.is.macos || electron_util_1.is.windows,
            type: 'checkbox',
            checked: electron_1.app.getLoginItemSettings().openAtLogin,
            click(menuItem) {
                electron_1.app.setLoginItemSettings({
                    openAtLogin: menuItem.checked,
                    openAsHidden: menuItem.checked
                });
            }
        },
        {
            label: 'Auto Hide Menu Bar',
            type: 'checkbox',
            visible: !electron_util_1.is.macos,
            checked: config_1.default.get('autoHideMenuBar'),
            click(menuItem, focusedWindow) {
                config_1.default.set('autoHideMenuBar', menuItem.checked);
                focusedWindow === null || focusedWindow === void 0 ? void 0 : focusedWindow.setAutoHideMenuBar(menuItem.checked);
                focusedWindow === null || focusedWindow === void 0 ? void 0 : focusedWindow.setMenuBarVisibility(!menuItem.checked);
                if (menuItem.checked) {
                    electron_1.dialog.showMessageBox({
                        type: 'info',
                        message: 'Press the Alt key to toggle the menu bar.',
                        buttons: ['OK']
                    });
                }
            }
        },
        {
            label: 'Flash Window on Message',
            type: 'checkbox',
            visible: !electron_util_1.is.macos,
            checked: config_1.default.get('flashWindowOnMessage'),
            click(menuItem) {
                config_1.default.set('flashWindowOnMessage', menuItem.checked);
            }
        },
        {
            id: 'showTrayIcon',
            label: 'Show Tray Icon',
            type: 'checkbox',
            enabled: (electron_util_1.is.linux || electron_util_1.is.windows) && !config_1.default.get('launchMinimized'),
            checked: config_1.default.get('showTrayIcon'),
            click() {
                util_1.toggleTrayIcon();
            }
        },
        {
            label: 'Launch Minimized',
            type: 'checkbox',
            visible: !electron_util_1.is.macos,
            checked: config_1.default.get('launchMinimized'),
            click() {
                util_1.toggleLaunchMinimized(menu);
            }
        },
        {
            label: 'Quit on Window Close',
            type: 'checkbox',
            checked: config_1.default.get('quitOnWindowClose'),
            click() {
                config_1.default.set('quitOnWindowClose', !config_1.default.get('quitOnWindowClose'));
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Advanced',
            submenu: advancedSubmenu
        }
    ];
    const viewSubmenu = [
        {
            label: 'Reset Text Size',
            accelerator: 'CommandOrControl+0',
            click() {
                util_1.sendAction('zoom-reset');
            }
        },
        {
            label: 'Increase Text Size',
            accelerator: 'CommandOrControl+Plus',
            click() {
                util_1.sendAction('zoom-in');
            }
        },
        {
            label: 'Decrease Text Size',
            accelerator: 'CommandOrControl+-',
            click() {
                util_1.sendAction('zoom-out');
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Follow System Appearance',
            type: 'checkbox',
            visible: electron_util_1.is.macos,
            checked: config_1.default.get('followSystemAppearance'),
            async click() {
                config_1.default.set('followSystemAppearance', !config_1.default.get('followSystemAppearance'));
                util_1.sendAction('set-dark-mode');
                await updateMenu();
            }
        },
        {
            label: 'Dark Mode',
            id: 'darkMode',
            type: 'checkbox',
            checked: config_1.default.get('darkMode'),
            enabled: !electron_util_1.is.macos || !config_1.default.get('followSystemAppearance'),
            accelerator: 'CommandOrControl+D',
            click() {
                config_1.default.set('darkMode', !config_1.default.get('darkMode'));
                util_1.sendAction('set-dark-mode');
            }
        },
        {
            label: 'Vibrancy',
            visible: electron_util_1.is.macos,
            submenu: vibrancySubmenu
        },
        {
            type: 'separator'
        },
        {
            label: 'Hide Names and Avatars',
            id: 'privateMode',
            type: 'checkbox',
            checked: config_1.default.get('privateMode'),
            accelerator: 'CommandOrControl+Shift+N',
            async click(menuItem, _browserWindow, event) {
                if (!config_1.default.get('privateMode') && config_1.default.get('showPrivateModePrompt') && event.shiftKey) {
                    const result = await electron_1.dialog.showMessageBox(_browserWindow, {
                        message: 'Are you sure you want to hide names and avatars?',
                        detail: 'This was triggered by Command/Control+Shift+N.',
                        buttons: [
                            'Hide',
                            'Don\'t Hide'
                        ],
                        defaultId: 0,
                        cancelId: 1,
                        checkboxLabel: 'Don\'t ask me again'
                    });
                    config_1.default.set('showPrivateModePrompt', !result.checkboxChecked);
                    if (result.response === 0) {
                        config_1.default.set('privateMode', !config_1.default.get('privateMode'));
                        util_1.sendAction('set-private-mode');
                    }
                    else if (result.response === 1) {
                        menuItem.checked = false;
                    }
                }
                else {
                    config_1.default.set('privateMode', !config_1.default.get('privateMode'));
                    util_1.sendAction('set-private-mode');
                }
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Sidebar',
            submenu: sidebarSubmenu
        },
        {
            label: 'Show Message Buttons',
            type: 'checkbox',
            checked: config_1.default.get('showMessageButtons'),
            click() {
                config_1.default.set('showMessageButtons', !config_1.default.get('showMessageButtons'));
                util_1.sendAction('toggle-message-buttons');
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Show Active Contacts',
            click() {
                util_1.sendAction('show-active-contacts-view');
            }
        },
        {
            label: 'Show Message Requests',
            click() {
                util_1.sendAction('show-message-requests-view');
            }
        },
        {
            label: 'Show Hidden Threads',
            click() {
                util_1.sendAction('show-hidden-threads-view');
            }
        },
        {
            label: 'Toggle Unread Threads',
            click() {
                util_1.sendAction('toggle-unread-threads-view');
            }
        }
    ];
    const spellCheckerSubmenu = spell_checker_1.default();
    const conversationSubmenu = [
        {
            label: 'Mute Conversation',
            accelerator: 'CommandOrControl+Shift+M',
            click() {
                util_1.sendAction('mute-conversation');
            }
        },
        {
            label: 'Hide Conversation',
            accelerator: 'CommandOrControl+Shift+H',
            click() {
                util_1.sendAction('hide-conversation');
            }
        },
        {
            label: 'Delete Conversation',
            accelerator: 'CommandOrControl+Shift+D',
            click() {
                util_1.sendAction('delete-conversation');
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Select Next Conversation',
            accelerator: 'Control+Tab',
            click() {
                util_1.sendAction('next-conversation');
            }
        },
        {
            label: 'Select Previous Conversation',
            accelerator: 'Control+Shift+Tab',
            click() {
                util_1.sendAction('previous-conversation');
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Find Conversation',
            accelerator: 'CommandOrControl+K',
            click() {
                util_1.sendAction('find');
            }
        },
        {
            label: 'Search in Conversation',
            accelerator: 'CommandOrControl+F',
            click() {
                util_1.sendAction('search');
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Insert GIF',
            accelerator: 'CommandOrControl+G',
            click() {
                util_1.sendAction('insert-gif');
            }
        },
        {
            label: 'Insert Sticker',
            accelerator: 'CommandOrControl+S',
            click() {
                util_1.sendAction('insert-sticker');
            }
        },
        {
            label: 'Insert Emoji',
            accelerator: 'CommandOrControl+E',
            click() {
                util_1.sendAction('insert-emoji');
            }
        },
        {
            label: 'Attach Files',
            accelerator: 'CommandOrControl+T',
            click() {
                util_1.sendAction('attach-files');
            }
        },
        {
            label: 'Focus Text Input',
            accelerator: 'CommandOrControl+I',
            click() {
                util_1.sendAction('focus-text-input');
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Spell Checker Language',
            visible: !electron_util_1.is.macos && config_1.default.get('isSpellCheckerEnabled'),
            submenu: spellCheckerSubmenu
        }
    ];
    const helpSubmenu = [
        electron_util_1.openUrlMenuItem({
            label: 'Website',
            url: 'https://sindresorhus.com/caprine'
        }),
        electron_util_1.openUrlMenuItem({
            label: 'Source Code',
            url: 'https://github.com/sindresorhus/caprine'
        }),
        electron_util_1.openUrlMenuItem({
            label: 'Donate…',
            url: 'https://github.com/sindresorhus/caprine?sponsor=1'
        }),
        {
            label: 'Report an Issue…',
            click() {
                const body = `
<!-- Please succinctly describe your issue and steps to reproduce it. -->


---

${electron_util_1.debugInfo()}`;
                electron_util_1.openNewGitHubIssue({
                    user: 'sindresorhus',
                    repo: 'caprine',
                    body
                });
            }
        }
    ];
    if (!electron_util_1.is.macos) {
        helpSubmenu.push({
            type: 'separator'
        }, electron_util_1.aboutMenuItem({
            icon: constants_1.caprineIconPath,
            text: 'Created by Sindre Sorhus'
        }));
    }
    const debugSubmenu = [
        {
            label: 'Show Settings',
            click() {
                config_1.default.openInEditor();
            }
        },
        {
            label: 'Show App Data',
            click() {
                electron_1.shell.openPath(electron_1.app.getPath('userData'));
            }
        },
        {
            type: 'separator'
        },
        {
            label: 'Delete Settings',
            click() {
                config_1.default.clear();
                electron_1.app.relaunch();
                electron_1.app.quit();
            }
        },
        {
            label: 'Delete App Data',
            click() {
                electron_1.shell.moveItemToTrash(electron_1.app.getPath('userData'));
                electron_1.app.relaunch();
                electron_1.app.quit();
            }
        }
    ];
    const macosTemplate = [
        electron_util_1.appMenu([
            {
                label: 'Caprine Preferences',
                submenu: preferencesSubmenu
            },
            {
                label: 'Messenger Preferences…',
                accelerator: 'Command+,',
                click() {
                    util_1.sendAction('show-preferences');
                }
            },
            {
                type: 'separator'
            },
            ...switchItems,
            {
                type: 'separator'
            },
            {
                label: 'Relaunch Caprine',
                click() {
                    electron_1.app.relaunch();
                    electron_1.app.quit();
                }
            }
        ]),
        {
            role: 'fileMenu',
            submenu: [
                newConversationItem,
                {
                    type: 'separator'
                },
                {
                    role: 'close'
                }
            ]
        },
        {
            role: 'editMenu'
        },
        {
            role: 'viewMenu',
            submenu: viewSubmenu
        },
        {
            label: 'Conversation',
            submenu: conversationSubmenu
        },
        {
            role: 'windowMenu'
        },
        {
            role: 'help',
            submenu: helpSubmenu
        }
    ];
    const linuxWindowsTemplate = [
        {
            role: 'fileMenu',
            submenu: [
                newConversationItem,
                {
                    type: 'separator'
                },
                {
                    label: 'Caprine Settings',
                    submenu: preferencesSubmenu
                },
                {
                    label: 'Messenger Settings',
                    accelerator: 'Control+,',
                    click() {
                        util_1.sendAction('show-preferences');
                    }
                },
                {
                    type: 'separator'
                },
                ...switchItems,
                {
                    type: 'separator'
                },
                {
                    label: 'Relaunch Caprine',
                    click() {
                        electron_1.app.relaunch();
                        electron_1.app.quit();
                    }
                },
                {
                    role: 'quit'
                }
            ]
        },
        {
            role: 'editMenu'
        },
        {
            role: 'viewMenu',
            submenu: viewSubmenu
        },
        {
            label: 'Conversation',
            submenu: conversationSubmenu
        },
        {
            role: 'help',
            submenu: helpSubmenu
        }
    ];
    const template = electron_util_1.is.macos ? macosTemplate : linuxWindowsTemplate;
    if (electron_util_1.is.development) {
        template.push({
            label: 'Debug',
            submenu: debugSubmenu
        });
    }
    const menu = electron_1.Menu.buildFromTemplate(template);
    electron_1.Menu.setApplicationMenu(menu);
    return menu;
}
exports.default = updateMenu;
