(() => {
    /**
     * 统一管理素材管理的键盘快捷键（复制自 client/js/disk/shortcut-keys.js）
     */
    class ResourceManagerShortcutKeys {
        constructor(options = {}) {
            this.selection = options.selection || null;
            this.onDelete = options.onDelete || null;
            this.onRename = options.onRename || null;
            this.onCopy = options.onCopy || null;
            this.onCut = options.onCut || null;
            this.onPaste = options.onPaste || null;

            this.handleKeyDown = this.handleKeyDown.bind(this);
            document.addEventListener('keydown', this.handleKeyDown);
            
            // 确保 iframe 可以接收键盘事件
            window.focus();
            console.log('[ShortcutKeys] 快捷键模块已初始化');
        }

        destroy() {
            document.removeEventListener('keydown', this.handleKeyDown);
        }

        handleKeyDown(e) {
            if (this.shouldIgnoreTarget(e.target)) {
                return;
            }

            // Delete
            if (e.key === 'Delete') {
                console.log('[ShortcutKeys] Delete 键被按下');
                console.log('[ShortcutKeys] this.selection:', this.selection);
                console.log('[ShortcutKeys] hasSelection:', this.selection ? this.selection.hasSelection() : 'N/A');
                
                if (this.selection && this.selection.hasSelection()) {
                    console.log('[ShortcutKeys] 有选中项，调用 onDelete');
                    e.preventDefault();
                    this.onDelete && this.onDelete();
                } else {
                    console.log('[ShortcutKeys] 没有选中项或 selection 为空');
                }
                return;
            }

            // 重命名
            if (e.key === 'F2') {
                if (this.selection && this.selection.getSelectedCount() === 1) {
                    e.preventDefault();
                    this.onRename && this.onRename();
                }
                return;
            }

            if (!e.ctrlKey) {
                return;
            }

            const key = e.key.toLowerCase();

            switch (key) {
                case 'a':
                    if (this.selection) {
                        e.preventDefault();
                        this.selection.selectAll();
                    }
                    break;
                case 'c':
                    if (this.selection && this.selection.hasSelection() && this.onCopy) {
                        e.preventDefault();
                        this.onCopy();
                    }
                    break;
                case 'x':
                    if (this.selection && this.selection.hasSelection() && this.onCut) {
                        e.preventDefault();
                        this.onCut();
                    }
                    break;
                case 'v':
                    if (this.onPaste) {
                        e.preventDefault();
                        Promise.resolve(this.onPaste()).catch((error) => {
                            console.error('粘贴失败:', error);
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        shouldIgnoreTarget(target) {
            if (!target) return false;
            const tag = target.tagName;
            if (!tag) return false;
            if (target.isContentEditable) return true;
            return tag === 'INPUT' || tag === 'TEXTAREA' || tag === 'SELECT';
        }
    }

    window.ResourceManagerShortcutKeys = ResourceManagerShortcutKeys;
})();

