function createPreferencesStorage({ DEFAULT_OPPORTUNITY_TYPE_VALUES, DEFAULT_OPPTY_PRODUCTS_CACHE_HOURS, FILTER_PREFERENCES_STORAGE_KEY, MODAL_ID, OPPORTUNITY_TYPE_OPTIONS, OPPORTUNITY_TYPE_PREFERENCES_STORAGE_KEY, OPPTY_PRODUCTS_CACHE_OPTIONS, PERIOD_OPTIONS, STAGE_OPTIONS, STATUS_OPTIONS, opportunitiesFilterState, environment }) { const { document } = environment; const getLocalStorage = () => environment.localStorage; function restoreFilterPreferences(select) { try { const rawPreferences = getLocalStorage().getItem(FILTER_PREFERENCES_STORAGE_KEY); if (!rawPreferences) { return; } const preferences = JSON.parse(rawPreferences); const savedStages = Array.isArray(preferences.stages) ? preferences.stages.filter((stage) => STAGE_OPTIONS.includes(stage)) : null; const savedStatuses = Array.isArray(preferences.statuses) ? preferences.statuses.filter((status) => STATUS_OPTIONS.includes(status)) : null; if (savedStages) { opportunitiesFilterState.selectedStages = new Set(savedStages); } if (savedStatuses) { opportunitiesFilterState.selectedStatuses = new Set(savedStatuses); } if (Array.isArray(preferences.customerKeys)) { opportunitiesFilterState.selectedCustomerKeys = new Set(preferences.customerKeys.map(String)); opportunitiesFilterState.hasSavedCustomerFilter = true; opportunitiesFilterState.customerFilterInitialized = true; } if (Array.isArray(preferences.ownerKeys)) { opportunitiesFilterState.selectedOwnerKeys = new Set(preferences.ownerKeys.map(String)); opportunitiesFilterState.hasSavedOwnerFilter = true; opportunitiesFilterState.ownerFilterInitialized = true; } opportunitiesFilterState.customerSearch = typeof preferences.customerSearch === "string" ? preferences.customerSearch : ""; opportunitiesFilterState.ownerSearch = typeof preferences.ownerSearch === "string" ? preferences.ownerSearch : ""; opportunitiesFilterState.tableSearch = typeof preferences.tableSearch === "string" ? preferences.tableSearch : ""; if (PERIOD_OPTIONS.includes(preferences.period)) { select.value = preferences.period; } } catch (error) { // Ignore malformed or unavailable browser storage and use default filters. } } function saveFilterPreferences(period) { try { const currentPeriod = period || document.querySelector(`#${MODAL_ID} select`)?.value || PERIOD_OPTIONS[0]; getLocalStorage().setItem(FILTER_PREFERENCES_STORAGE_KEY, JSON.stringify({ period: currentPeriod, stages: Array.from(opportunitiesFilterState.selectedStages), statuses: Array.from(opportunitiesFilterState.selectedStatuses), customerKeys: opportunitiesFilterState.hasSavedCustomerFilter ? Array.from(opportunitiesFilterState.selectedCustomerKeys) : null, ownerKeys: opportunitiesFilterState.hasSavedOwnerFilter ? Array.from(opportunitiesFilterState.selectedOwnerKeys) : null, customerSearch: opportunitiesFilterState.customerSearch, ownerSearch: opportunitiesFilterState.ownerSearch, tableSearch: opportunitiesFilterState.tableSearch })); } catch (error) { // Ignore unavailable browser storage; filters still work for the current modal. } } function getSavedOpportunityTypeValues() { try { const rawPreferences = getLocalStorage().getItem(OPPORTUNITY_TYPE_PREFERENCES_STORAGE_KEY); if (!rawPreferences) { return DEFAULT_OPPORTUNITY_TYPE_VALUES; } const preferences = JSON.parse(rawPreferences); const values = Array.isArray(preferences.opportunityTypeValues) ? preferences.opportunityTypeValues : null; if (!values) { return DEFAULT_OPPORTUNITY_TYPE_VALUES; } const allowedValues = new Set(OPPORTUNITY_TYPE_OPTIONS.map((option) => option.value)); return values.filter((value) => allowedValues.has(value)); } catch (error) { return DEFAULT_OPPORTUNITY_TYPE_VALUES; } } function getSavedOpptyProductsCacheHours() { try { const rawPreferences = getLocalStorage().getItem(OPPORTUNITY_TYPE_PREFERENCES_STORAGE_KEY); if (!rawPreferences) { return DEFAULT_OPPTY_PRODUCTS_CACHE_HOURS; } const preferences = JSON.parse(rawPreferences); const cacheHours = Number(preferences.opptyProductsCacheHours); return OPPTY_PRODUCTS_CACHE_OPTIONS.some((option) => option.value === cacheHours) ? cacheHours : DEFAULT_OPPTY_PRODUCTS_CACHE_HOURS; } catch (error) { return DEFAULT_OPPTY_PRODUCTS_CACHE_HOURS; } } return { restoreFilterPreferences, saveFilterPreferences, getSavedOpportunityTypeValues, getSavedOpptyProductsCacheHours }; }