import { DEFAULT_SETTINGS } from "./core/model.js";
const form = document.querySelector("#settings");
const status = document.querySelector("#status");
const resetLayout = document.querySelector("#reset-layout");
void load();
form.addEventListener("change", (event) => void save(event.target.name !== "autoTranslate"));
form.elements.namedItem("fontScale").addEventListener("input", () => void save(false));
resetLayout.onclick = () => void reset();
async function load() {
    const settings = { ...DEFAULT_SETTINGS, ...await chrome.storage.sync.get(DEFAULT_SETTINGS) };
    for (const [key, value] of Object.entries(settings)) {
        const input = form.elements.namedItem(key);
        if (input)
            input.type === "checkbox" ? input.checked = value === true : input.value = String(value);
    }
}
async function save(restart) {
    const data = new FormData(form);
    const inputMode = data.get("inputMode");
    const sourceLanguage = data.get("sourceLanguage");
    const autoTranslate = form.elements.namedItem("autoTranslate").checked;
    await chrome.storage.sync.set({ inputMode, sourceLanguage, targetLanguage: "zh-Hans", localAsrProfile: "realtime", autoTranslate, fontScale: Number(data.get("fontScale")), localSettings: { inputMode, sourceLanguage, localAsrProfile: "realtime", autoTranslate } });
    await chrome.runtime.sendMessage({ type: restart ? "m1.settings.changed" : "m1.settings.apply" });
    status.value = "设置已自动保存。";
}
async function reset() { await chrome.runtime.sendMessage({ type: "m1.overlay.reset" }); status.value = "字幕窗已恢复到底部默认布局。"; }
