const OFFLINE_DATA_KEY = "offlineData";
const USER_ITEMS_KEY = "userItems";
const SETTINGS_KEY = "settings";
const NOTIFICATION_ALARM = "bacdzx-periodic-reminder";
async function loadPackagedData() {
 const response = await fetch(chrome.runtime.getURL("data/index.json"));
 if (!response.ok) return null;
 return response.json();
}
async function seedPackagedData() {
 const packagedData = await loadPackagedData();
 if (!packagedData) return;
 const current = await chrome.storage.local.get([OFFLINE_DATA_KEY]);
 if (!current.offlineData || current.offlineData.version !== packagedData.version) {
 await chrome.storage.local.set({ [OFFLINE_DATA_KEY]: packagedData });
 }
}
async function ensureDefaults() {
 const { settings } = await chrome.storage.local.get([SETTINGS_KEY]);
 if (settings) return;
 await chrome.storage.local.set({
 [SETTINGS_KEY]: {
 notifications: {
 enabled: false,
 intervalHours: 6,
 type: "all",
 module: "",
 unit: ""
 }
 }
 });
}
async function getMergedItems() {
 const stored = await chrome.storage.local.get([OFFLINE_DATA_KEY, USER_ITEMS_KEY]);
 const packagedData = stored.offlineData || await loadPackagedData();
 const userItems = stored.userItems || [];
 return [...(packagedData?.items || []), ...userItems];
}
function filterNotificationItems(items, notifications) {
 return items.filter((item) => {
 if (item.kind === "subject") return false;
 if (notifications.type !== "all" && item.kind !== notifications.type) return false;
 if (notifications.module && item.module !== notifications.module) return false;
 if (notifications.unit && item.unit !== notifications.unit) return false;
 return true;
 });
}
async function scheduleNotifications() {
 const { settings } = await chrome.storage.local.get([SETTINGS_KEY]);
 const notifications = settings?.notifications;
 await chrome.alarms.clear(NOTIFICATION_ALARM);
 if (!notifications?.enabled) return;
 const intervalHours = Math.max(1, Number(notifications.intervalHours || 6));
 await chrome.alarms.create(NOTIFICATION_ALARM, {
 delayInMinutes: 1,
 periodInMinutes: intervalHours * 60
 });
}
async function showReminderNotification() {
 const { settings } = await chrome.storage.local.get([SETTINGS_KEY]);
 const notifications = settings?.notifications || {};
 const items = await getMergedItems();
 const matches = filterNotificationItems(items, notifications);
 if (!matches.length) return;
 const item = matches[Math.floor(Math.random() * matches.length)];
 const prefix = item.kind === "date" ? "تاريخ للمراجعة" : item.kind === "person" ? "شخصية للمراجعة" : "مصطلح للمراجعة";
 await chrome.notifications.create({
 type: "basic",
 iconUrl: chrome.runtime.getURL("assets/logo.png"),
 title: `${prefix}: ${item.title}`,
 message: item.body.slice(0, 220),
 priority: 1
 });
}
async function initialize() {
 await ensureDefaults();
 await seedPackagedData();
 await scheduleNotifications();
}
chrome.runtime.onInstalled.addListener(() => {
 initialize();
});
chrome.runtime.onStartup.addListener(() => {
 initialize();
});
chrome.storage.onChanged.addListener((changes, areaName) => {
 if (areaName !== "local") return;
 if (changes.settings || changes.offlineData || changes.userItems) {
 scheduleNotifications();
 }
});
chrome.alarms.onAlarm.addListener((alarm) => {
 if (alarm.name === NOTIFICATION_ALARM) {
 showReminderNotification();
 }
});