import { logger } from '../logger' import { IIntentSource, IIntentSourcesOptions } from './types' export function getStorage(lsKey: string) { try { const item = localStorage.getItem(lsKey) return JSON.parse(item || '') } catch { return null } } export const verifyOptions = (options: unknown): boolean => { if (typeof options !== 'object') { logger.error( `Error loading Dreamdata script: 'intentSources' option must be an object but got ${typeof options}. This means there is likely an error in your script setup.` ) return false } if (options === null) { logger.error( 'Failed to parse intent sources options due to a bug in our script. Please contact Dreamdata support.' ) return false } if (Object.keys(options).length === 0) { logger.error( "Error loading Dreamdata script: 'intentSources' cannot be empty if enabled. This means there is likely an error in your script setup." ) return false } return true } export const getMatchingSources = ( sources: IIntentSource[], options: IIntentSourcesOptions ) => { const matchingSources: IIntentSource[] = [] for (const [name, enabled] of Object.entries(options)) { if (!enabled) { continue } const foundSource = sources.find((source) => source.name === name) if (foundSource) { matchingSources.push(foundSource) } else { logger.error( `Unable to find intent source '${name}'. This means you have included an intent source in your script setup that is not recognized by Dreamdata.` ) } } return matchingSources }