"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSequenceContextMenuItems = void 0;
const no_react_1 = require("remotion/no-react");
const NotificationCenter_1 = require("../Notifications/NotificationCenter");
const timeline_asset_link_1 = require("./timeline-asset-link");
const normalizeMenuDividers = (items) => {
    var _a;
    const normalized = [];
    for (const item of items) {
        if (item.type === 'divider') {
            const previousItem = normalized[normalized.length - 1];
            if (!previousItem || previousItem.type === 'divider') {
                continue;
            }
        }
        normalized.push(item);
    }
    if (((_a = normalized[normalized.length - 1]) === null || _a === void 0 ? void 0 : _a.type) === 'divider') {
        normalized.pop();
    }
    return normalized;
};
const interactiveSvgComponentIdentity = 'dev.remotion.remotion.Interactive.Svg';
const getSequenceContextMenuItems = ({ assetLinkInfo, canOpenInEditor, deleteDisabled, disableInteractivityDisabled, duplicateDisabled, fileLocation, includeSourceEditItems, onDeleteSequenceFromSource, onDisableSequenceInteractivity, onDuplicateSequenceFromSource, openInEditor, originalLocation, selectAsset, sequence, sourceActions = [], }) => {
    var _a, _b;
    const editorName = window.remotion_editorName;
    const { documentationLink } = sequence;
    const isInteractiveSvg = ((_a = sequence.controls) === null || _a === void 0 ? void 0 : _a.componentIdentity) === interactiveSvgComponentIdentity;
    const items = [
        editorName
            ? {
                type: 'item',
                id: 'show-in-editor',
                keyHint: null,
                label: `Show in ${editorName}`,
                leftItem: null,
                disabled: !canOpenInEditor || !originalLocation,
                onClick: openInEditor,
                quickSwitcherLabel: null,
                subMenu: null,
                value: 'show-in-editor',
            }
            : null,
        {
            type: 'item',
            id: 'copy-file-location',
            keyHint: null,
            label: 'Copy file location',
            leftItem: null,
            disabled: !fileLocation,
            onClick: () => {
                if (!fileLocation) {
                    return;
                }
                navigator.clipboard
                    .writeText(fileLocation)
                    .then(() => {
                    (0, NotificationCenter_1.showNotification)('Copied file location to clipboard', 1000);
                })
                    .catch((err) => {
                    (0, NotificationCenter_1.showNotification)(`Could not copy to clipboard: ${err.message}`, 1000);
                });
            },
            quickSwitcherLabel: null,
            subMenu: null,
            value: 'copy-file-location',
        },
        documentationLink
            ? {
                type: 'item',
                id: 'open-component-docs',
                keyHint: null,
                label: 'Open component docs',
                leftItem: null,
                disabled: false,
                onClick: () => {
                    window.open(documentationLink, '_blank', 'noopener,noreferrer');
                },
                quickSwitcherLabel: null,
                subMenu: null,
                value: 'open-component-docs',
            }
            : null,
        assetLinkInfo
            ? {
                type: 'item',
                id: 'show-asset',
                keyHint: null,
                label: 'Show asset',
                leftItem: null,
                disabled: false,
                onClick: () => {
                    (0, timeline_asset_link_1.openTimelineAssetLink)(assetLinkInfo, selectAsset);
                },
                quickSwitcherLabel: null,
                subMenu: null,
                value: 'show-asset',
            }
            : null,
        documentationLink || assetLinkInfo
            ? {
                type: 'divider',
                id: 'sequence-link-divider',
            }
            : null,
        isInteractiveSvg
            ? {
                type: 'item',
                id: 'copy-svg',
                keyHint: null,
                label: 'Copy SVG',
                leftItem: null,
                disabled: !((_b = sequence.refForOutline) === null || _b === void 0 ? void 0 : _b.current),
                onClick: () => {
                    var _a;
                    const svg = (_a = sequence.refForOutline) === null || _a === void 0 ? void 0 : _a.current;
                    if (!svg) {
                        return;
                    }
                    navigator.clipboard
                        .writeText(svg.outerHTML)
                        .then(() => {
                        (0, NotificationCenter_1.showNotification)('Copied SVG to clipboard', 1000);
                    })
                        .catch((err) => {
                        (0, NotificationCenter_1.showNotification)(`Could not copy to clipboard: ${err.message}`, 1000);
                    });
                },
                quickSwitcherLabel: null,
                subMenu: null,
                value: 'copy-svg',
            }
            : null,
        isInteractiveSvg
            ? {
                type: 'divider',
                id: 'copy-svg-divider',
            }
            : null,
        sourceActions.length > 0
            ? {
                type: 'divider',
                id: 'sequence-source-actions-divider',
            }
            : null,
        ...sourceActions,
        {
            type: 'item',
            id: 'disable-interactivity',
            keyHint: null,
            label: 'Disable interactivity',
            leftItem: null,
            disabled: disableInteractivityDisabled,
            onClick: onDisableSequenceInteractivity,
            quickSwitcherLabel: null,
            subMenu: null,
            value: 'disable-interactivity',
        },
        includeSourceEditItems
            ? {
                type: 'item',
                id: 'duplicate-sequence',
                keyHint: null,
                label: 'Duplicate',
                leftItem: null,
                disabled: duplicateDisabled,
                onClick: onDuplicateSequenceFromSource,
                quickSwitcherLabel: null,
                subMenu: null,
                value: 'duplicate-sequence',
            }
            : null,
        includeSourceEditItems
            ? {
                type: 'divider',
                id: 'sequence-duplicate-delete-divider',
            }
            : null,
        includeSourceEditItems
            ? {
                type: 'item',
                id: 'delete-sequence',
                keyHint: null,
                label: 'Delete',
                leftItem: null,
                disabled: deleteDisabled,
                onClick: onDeleteSequenceFromSource,
                quickSwitcherLabel: null,
                subMenu: null,
                value: 'delete-sequence',
            }
            : null,
    ].filter(no_react_1.NoReactInternals.truthy);
    return normalizeMenuDividers(items);
};
exports.getSequenceContextMenuItems = getSequenceContextMenuItems;
