import { jsx as _jsx } from "react/jsx-runtime";
import { sanitizeSvg } from "../../lib/sanitize";
/**
 * Renders an SVG icon from markup string
 */
function SvgIcon({ svgMarkup, className, }) {
    return (_jsx("span", { className: className, dangerouslySetInnerHTML: { __html: sanitizeSvg(svgMarkup) } }));
}
/**
 * Find the handler for a given command type
 */
export function findHandler(commandType, configuration) {
    return configuration.customCommands?.handlers.find((h) => h.type === commandType);
}
export function isCustomCommandDisabled(command, configuration, context) {
    const handler = findHandler(command.commandType, configuration);
    if (!handler) {
        return true;
    }
    return handler.disabled(command, context);
}
export async function executeCustomCommand(command, configuration, context) {
    const handler = findHandler(command.commandType, configuration);
    if (!handler) {
        console.warn(`No handler registered for custom command type: ${command.commandType}`);
        return false;
    }
    if (handler.disabled(command, context)) {
        return false;
    }
    await handler.execute(command, context);
    return true;
}
export function renderCustomCommandIcon(command, configuration, options) {
    if (command.svgIcon) {
        return _jsx(SvgIcon, { svgMarkup: command.svgIcon, className: options?.svgClassName });
    }
    const handler = findHandler(command.commandType, configuration);
    if (handler?.renderIcon) {
        return handler.renderIcon(command);
    }
    return options?.fallback ?? null;
}
/**
 * Convert CustomCommandData + Handler into a ComponentCommand.
 * This allows custom commands to work with the existing component command infrastructure.
 */
export function toComponentCommand(data, handler, components, visibilityScopes = ["editFrame", "contextMenu"]) {
    return {
        id: `custom-${data.commandType}-${data.id}`,
        label: data.label,
        icon: data.svgIcon
            ? _jsx(SvgIcon, { svgMarkup: data.svgIcon, className: "h-4 w-4" })
            : handler.renderIcon
                ? handler.renderIcon(data)
                : null,
        visibilityScopes,
        disabled: (context) => {
            const customContext = {
                editContext: context.editContext,
                event: context.event,
                components,
            };
            return handler.disabled(data, customContext);
        },
        execute: async (context) => {
            const customContext = {
                editContext: context.editContext,
                event: context.event,
                components,
            };
            await handler.execute(data, customContext);
        },
    };
}
/**
 * Load and convert custom commands for a specific use case.
 * Returns ComponentCommands that can be used in frame menu, context menu, etc.
 */
export async function getCustomComponentCommands(components, editContext, loadCommands, visibilityScopes = ["editFrame", "contextMenu"]) {
    const configuration = editContext.configuration;
    const handlers = configuration?.customCommands?.handlers ?? [];
    if (handlers.length === 0) {
        return [];
    }
    try {
        const commandsData = await loadCommands();
        return commandsData
            .map((data) => {
            const handler = handlers.find((h) => h.type === data.commandType);
            if (!handler) {
                console.warn(`No handler registered for custom command type: ${data.commandType}`);
                return null;
            }
            return toComponentCommand(data, handler, components, visibilityScopes);
        })
            .filter((cmd) => cmd !== null);
    }
    catch (error) {
        console.error("Error loading custom commands:", error);
        return [];
    }
}
//# sourceMappingURL=customCommandConverter.js.map