import { Storage } from '@dilvant/front-lib'; import { STORAGE_KEYS } from './../constants/STORAGE'; import { IApp } from './../interfaces/IApp'; import { IMnSeo, IOgSeo, IOnSeo, ISeo, IStSeo, ITwitterSeo, } from './../interfaces/ISeo'; import { InObjectLookup } from './InObjectLookup'; export class InAppLookup { static async find(searchValue: any) { const app = await Storage.get(STORAGE_KEYS.APP); return InObjectLookup.find(app, searchValue); } static async findAndReplace( searchValue: any, newValue: any, keys: string[] = ['value'], safe?: { parent: string; key: string; access?: string } ) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); const itemFound = InObjectLookup.find(app, searchValue); if (itemFound) { keys.forEach((key) => (itemFound[key] = newValue[key])); app.sections!.forEach((APP) => { if (!APP.elements) return; APP.elements.forEach((elem: any) => { if (!elem.attributes.events) return; if (elem.attributes.events[0]._id === itemFound._id) elem.attributes.params = []; }); }); } else if (safe?.parent && safe?.key) { const itemFinded = InObjectLookup.find(app, safe.parent); if (safe.access) { if (!itemFinded[safe.access]) itemFinded[safe.access] = { [safe.key]: [newValue], }; else if (!itemFinded[safe.access][safe.key]) { itemFinded[safe.access][safe.key] = [newValue]; } else { itemFinded[safe.access][safe.key].push(newValue); } } else { if (!itemFinded[safe.key]) itemFinded[safe.key] = [newValue]; else itemFinded[safe.key].push(newValue); } } else return; let preApp = JSON.parse(JSON.stringify(app)); return await Storage.set(STORAGE_KEYS.APP, preApp); } static async findAndReplaceArray( newValue: any[], keys: string[] = ['value'], safe?: { parent: string; key: string; access?: string } ) { let recorrido = newValue.length; const app: IApp = await Storage.get(STORAGE_KEYS.APP); newValue.forEach((item) => { if (!item._id) return; const itemFound = InObjectLookup.find(app, item._id); if (itemFound) { keys.forEach((key) => { itemFound[key] = item[key]; }); } else if (safe?.parent && safe?.key) { const itemFinded = InObjectLookup.find(app, safe.parent); if (safe.access) { if (!itemFinded[safe.access]) itemFinded[safe.access] = { [safe.key]: [item], }; else if (!itemFinded[safe.access][safe.key]) { itemFinded[safe.access][safe.key] = [item]; } else { if (safe.key === 'params') { if (itemFinded[safe.access][safe.key].length === 1) { if ( itemFinded[safe.access][safe.key][0].key === 'vCardId' || recorrido === 2 ) { itemFinded[safe.access][safe.key] = []; recorrido--; } itemFinded[safe.access][safe.key].push(item); } else if (itemFinded[safe.access][safe.key].length === 2) { itemFinded[safe.access][safe.key] = []; itemFinded[safe.access][safe.key].push(item); recorrido--; } else itemFinded[safe.access][safe.key].push(item); } else itemFinded[safe.access][safe.key].push(item); } } else { if (!itemFinded[safe.key]) itemFinded[safe.key] = [item]; else itemFinded[safe.key].push(item); } } else return; }); return await Storage.set(STORAGE_KEYS.APP, app); } static async findAndReplaceCompleteArrays( searchValue: any, newValue: any[], key: string ) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); const foundItem: any = await InObjectLookup.find(app, searchValue); foundItem[key] = newValue; return await Storage.set(STORAGE_KEYS.APP, app); } static async addItem(searchValue: any, newItem: any, key: string) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); const itemFinded = InObjectLookup.find(app, searchValue); if (!itemFinded[key] || !Array.isArray(itemFinded[key])) return; itemFinded[key].push(newItem); return await Storage.set(STORAGE_KEYS.APP, app); } static async addObject(searchValue: any, newItem: any, key: string) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); const itemFinded = InObjectLookup.find(app, searchValue); if (itemFinded[key]) return; itemFinded[key] = newItem; return await Storage.set(STORAGE_KEYS.APP, app); } static async removeItem(searchValue: any, item: any, key: string) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); const itemFinded: any = InObjectLookup.find(app, searchValue); if (!itemFinded[key] || !Array.isArray(itemFinded[key])) return; itemFinded[key] = itemFinded[key].filter((i: any) => i._id !== item._id); return await Storage.set(STORAGE_KEYS.APP, app); } static async removeObject(searchValue: any, key: string) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); const itemFinded: any = InObjectLookup.find(app, searchValue); delete itemFinded[key]; return await Storage.set(STORAGE_KEYS.APP, app); } static validSafeAccess( itemFinded: any, safe: { parent: string; key: string; access?: string; } ) { let valid: boolean; if (safe.access) { valid = itemFinded[safe.access] && itemFinded[safe.access][safe.key] && Array.isArray(itemFinded[safe.access][safe.key]); } else { valid = itemFinded[safe.key] && Array.isArray(itemFinded[safe.key]); } return valid; } static async searchTheNearestSectionByElement(_id: string): Promise { const app: IApp = await Storage.get(STORAGE_KEYS.APP); const foundItem: any = await InObjectLookup.find(app, _id); let value = undefined; const foundSection = app.sections!.find((d) => d._id === foundItem._id); if (foundSection) value = foundSection._id; if (!foundItem.attributes) return undefined; if (!value) { const parentItem = foundItem.attributes.dataset.find( ({ key }: any) => key === 'parent' ); if (parentItem) { value = await InAppLookup.searchTheNearestSectionByElement( parentItem.value ); } } return value; } static async iconOfSidebar(_id: string) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); if (!app.nav) return false; const foundItem: any = await InObjectLookup.find(app.nav, _id); if (foundItem) return true; return false; } static async seoOptions( key: string, newValue: ISeo | IStSeo | IMnSeo | ITwitterSeo | IOnSeo | IOgSeo | any ) { const app: IApp = await Storage.get(STORAGE_KEYS.APP); (app.app)[key] = newValue; return await Storage.set(STORAGE_KEYS.APP, app); } }