// Copyright (c) 2025, Huawei Technologies Co., Ltd.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0  (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import { LogicalSize } from '@tauri-apps/api/dpi';
import { currentMonitor, type Monitor } from '@tauri-apps/api/window';
import NativeWindow from './native';

document.addEventListener(
    'wheel',
    e => (e.ctrlKey || e.metaKey) && Math.abs(e.deltaY) !== 0 && e.preventDefault(),
    { passive: false }
);
document.addEventListener('contextmenu', e => e.preventDefault(), { passive: false });
document.addEventListener('keydown', e => {
    // 检查目标元素是否在允许更多快捷键的容器内
    const isInsideAllowShortcuts = (e.target as HTMLElement)?.closest('[data-allow-shortcuts]');

    if (e.ctrlKey || e.metaKey) {
        if (isInsideAllowShortcuts && ['a', 'x'].includes(e.key.toLowerCase())) {
            return;
        } else {
            // 在普通元素上,只允许基本的复制粘贴
            if (['c', 'v'].includes(e.key.toLowerCase())) {
                return;
            }
        }
        e.preventDefault();
    }
}, {
    passive: false
});

const {
    size: { width, height },
    scaleFactor
} = await currentMonitor() as Monitor;
await Promise.all([
    NativeWindow.setSize(new LogicalSize(
        Math.max(Math.round(width * 0.65 / scaleFactor), 1100),
        Math.max(Math.round(height * 0.71 / scaleFactor), 720)
    )),
    NativeWindow.center(),
]);
await NativeWindow.maximize();
await NativeWindow.show();