import { loadOfflineData } from "./data.js";
import { buildTaxonomy } from "./search.js";
import {
 clearFavorites,
 clearLastSearch,
 getFavorites,
 getSettings,
 saveSettings
} from "./storage.js";
const elements = {
 notificationsEnabledSelect: document.getElementById("notificationsEnabledSelect"),
 notificationIntervalInput: document.getElementById("notificationIntervalInput"),
 notificationTypeSelect: document.getElementById("notificationTypeSelect"),
 notificationModuleSelect: document.getElementById("notificationModuleSelect"),
 notificationUnitSelect: document.getElementById("notificationUnitSelect"),
 saveSettingsBtn: document.getElementById("saveSettingsBtn"),
 resetSettingsBtn: document.getElementById("resetSettingsBtn"),
 clearHistoryBtn: document.getElementById("clearHistoryBtn"),
 clearFavoritesBtn: document.getElementById("clearFavoritesBtn"),
 settingsNotice: document.getElementById("settingsNotice"),
 dataStatus: document.getElementById("dataStatus")
};
const defaultSettings = {
 notifications: {
 enabled: false,
 intervalHours: 6,
 type: "all",
 module: "",
 unit: ""
 }
};
function populateSelect(select, values, placeholder) {
 select.innerHTML = "";
 const empty = document.createElement("option");
 empty.value = "";
 empty.textContent = placeholder;
 select.appendChild(empty);
 values.forEach((value) => {
 const option = document.createElement("option");
 option.value = value;
 option.textContent = value;
 select.appendChild(option);
 });
}
function notify(message) {
 elements.settingsNotice.textContent = message;
}
async function renderStatus() {
 const data = await loadOfflineData();
 const favorites = await getFavorites();
 const counts = (data.items || []).reduce((acc, item) => {
 acc[item.kind] = (acc[item.kind] || 0) + 1;
 return acc;
 }, {});
 elements.dataStatus.innerHTML = `
 <p>نسخة البيانات: <strong>${data.version}</strong></p>
 <p>مصطلحات: <strong>${counts.term || 0}</strong> | شخصيات: <strong>${counts.person || 0}</strong> | تواريخ: <strong>${counts.date || 0}</strong></p>
 <p>مواضيع منشورة: <strong>${counts.subject || 0}</strong> | مفضلة: <strong>${favorites.length}</strong></p>
 `;
}
async function init() {
 const settings = await getSettings();
 const data = await loadOfflineData();
 const taxonomy = buildTaxonomy(data);
 populateSelect(elements.notificationModuleSelect, taxonomy.modules, "كل المواد");
 populateSelect(elements.notificationUnitSelect, taxonomy.units, "كل الوحدات");
 elements.notificationsEnabledSelect.value = String(settings.notifications?.enabled || false);
 elements.notificationIntervalInput.value = String(settings.notifications?.intervalHours || 6);
 elements.notificationTypeSelect.value = settings.notifications?.type || "all";
 elements.notificationModuleSelect.value = settings.notifications?.module || "";
 elements.notificationUnitSelect.value = settings.notifications?.unit || "";
 await renderStatus();
}
elements.saveSettingsBtn.addEventListener("click", async () => {
 const next = {
 ...(await getSettings()),
 notifications: {
 enabled: elements.notificationsEnabledSelect.value === "true",
 intervalHours: Math.max(1, Number(elements.notificationIntervalInput.value || 6)),
 type: elements.notificationTypeSelect.value || "all",
 module: elements.notificationModuleSelect.value,
 unit: elements.notificationUnitSelect.value
 }
 };
 await saveSettings(next);
 notify("تم حفظ إعدادات الإشعارات.");
});
elements.resetSettingsBtn.addEventListener("click", async () => {
 elements.notificationsEnabledSelect.value = String(defaultSettings.notifications.enabled);
 elements.notificationIntervalInput.value = String(defaultSettings.notifications.intervalHours);
 elements.notificationTypeSelect.value = defaultSettings.notifications.type;
 elements.notificationModuleSelect.value = defaultSettings.notifications.module;
 elements.notificationUnitSelect.value = defaultSettings.notifications.unit;
 await saveSettings(defaultSettings);
 notify("تمت استعادة الإعدادات الافتراضية.");
});
elements.clearHistoryBtn.addEventListener("click", async () => {
 await clearLastSearch();
 notify("تم مسح آخر بحث.");
});
elements.clearFavoritesBtn.addEventListener("click", async () => {
 await clearFavorites();
 await renderStatus();
 notify("تم مسح المفضلة.");
});
init().catch((error) => {
 console.error(error);
 notify("تعذر تحميل الإعدادات.");
});