import ExtensionSettings,{VIDEO_SETTINGS,TOOLBAR_SETTINGS,SHORTCUTS_SETTINGS,CHANNEL_PAGE_SETTINGS,WATCH_PAGE_SETTINGS,YOUTUBE_STUDIO_SETTINGS}from"@/js/lib/ExtensionSettings";import{queryClient}from"./features/shared/query-client";function updateCheckbox(t,e){const n=document.getElementById(t);n&&(n.checked=e)}async function updateSubSettings(t,e,n=(t=>t)){const i=t;i.forEach((t=>{updateCheckbox(n(t),e)}));const a=Object.fromEntries(i.map((t=>[n(t),e])));await ExtensionSettings.setMany(a)}async function updateSetting(t,e){if("home-page-shortcuts"!==t&&"video-manager-shortcuts"!==t||await updateSubSettings(SHORTCUTS_SETTINGS,e,(e=>`${t}-${e}`)),"toolbar"===t&&await updateSubSettings(TOOLBAR_SETTINGS,e),"video-settings"===t&&await updateSubSettings(VIDEO_SETTINGS,e),"watch-page"===t&&await updateSubSettings(WATCH_PAGE_SETTINGS,e),"channel-page-settings"===t&&await updateSubSettings(CHANNEL_PAGE_SETTINGS,e),"youtube-studio-settings"===t){const t=YOUTUBE_STUDIO_SETTINGS;t.forEach((t=>{updateCheckbox(t,e)}));const n=Object.fromEntries(t.map((t=>[t,e])));await ExtensionSettings.setMany(n)}updateCheckbox(t,e),await ExtensionSettings.set(t,e);if(TOOLBAR_SETTINGS.includes(t)){const t=getToolbarState(await ExtensionSettings.getSettings());updateCheckbox("toolbar",t),await ExtensionSettings.set("toolbar",t)}if(VIDEO_SETTINGS.includes(t)){const t=getVideoSettingsState(await ExtensionSettings.getSettings());updateCheckbox("video-settings",t),await ExtensionSettings.set("video-settings",t)}if(WATCH_PAGE_SETTINGS.includes(t)){const t=getWatchPageState(await ExtensionSettings.getSettings());updateCheckbox("watch-page",t),await ExtensionSettings.set("watch-page",t)}if(CHANNEL_PAGE_SETTINGS.includes(t)){const t=getChannelPageState(await ExtensionSettings.getSettings());updateCheckbox("channel-page-settings",t),await ExtensionSettings.set("channel-page-settings",t)}if(YOUTUBE_STUDIO_SETTINGS.includes(t)){const t=getYoutubeStudioState(await ExtensionSettings.getSettings());updateCheckbox("youtube-studio-settings",t),await ExtensionSettings.set("youtube-studio-settings",t)}await queryClient.invalidateQueries(["extension-setting"])}function getVideoSettingsState(t){return VIDEO_SETTINGS.some((e=>t[e]))}function getToolbarState(t){return TOOLBAR_SETTINGS.some((e=>t[e]))}function getChannelPageState(t){return CHANNEL_PAGE_SETTINGS.some((e=>t[e]))}function getWatchPageState(t){return WATCH_PAGE_SETTINGS.some((e=>t[e]))}function getYoutubeStudioState(t){return YOUTUBE_STUDIO_SETTINGS.some((e=>t[e]))}async function loadSettings(){const t=await ExtensionSettings.getSettings();updateCheckbox("video-settings",getVideoSettingsState(t));updateCheckbox("toolbar",getToolbarState(t));updateCheckbox("channel-page-settings",getChannelPageState(t));updateCheckbox("watch-page",getWatchPageState(t)),Object.keys(t).forEach((e=>{updateCheckbox(e,t[e])}))}function translate(){document.title=chrome.i18n.getMessage("vidIQVisionOptions"),document.querySelectorAll("[data-i18n]").forEach((function(t){const e=t.getAttribute("data-i18n");t.textContent=chrome.i18n.getMessage(e)}))}document.addEventListener("DOMContentLoaded",(function(){translate(),loadSettings();const t=document.getElementsByTagName("input");for(let e=0;e<t.length;e++)t[e].addEventListener("click",(function(){updateSetting(this.id,this.checked)}))}));