import { XM } from '@dilvant/xm-sdk'; // import { SDK } from '@dilvant/bizcard-sdk'; import { InAppLookup } from '../classes/InAppLookup'; import { SectionsSubscriber } from '../classes/SectionSubscriber'; import { IAttributeItem } from '../interfaces/IAttributes'; export class CrudAttribute { static async saveAttribute( attribute: IAttributeItem | any, load: boolean = true, key: string[] = ['value'] ) { if (!attribute) return; const { section, parent, type } = attribute; delete attribute.section; attribute = await XM.Attribute.saveById(attribute); const keyType = { style: 'styles', class: 'classes', util: 'utils', events: 'events', param: 'params', }; const safe = { parent, key: (keyType)[type] || 'classes', access: 'attributes', }; const save = await InAppLookup.findAndReplace( attribute._id, attribute, key, safe ); if (load && section) SectionsSubscriber.refresh(section); return attribute; } static async saveAttributes( attributes: IAttributeItem[] | any, section?: any ) { if (!attributes) return; let { parent, type } = attributes[0]; attributes = await XM.Attribute.saveAttributes(attributes); const safe = { parent, key: type === 'style' ? 'styles' : 'classes', access: 'attributes', }; await InAppLookup.findAndReplaceArray(attributes, ['value'], safe); section && SectionsSubscriber.refresh(section); return attributes; } static async saveAttributeById(attribute: IAttributeItem | any) { if (!attribute._id) return; let { section } = attribute; delete attribute.section; attribute = await XM.Attribute.saveById(attribute, true); await InAppLookup.findAndReplace(attribute._id, attribute, [ 'value', 'key', ]); section && SectionsSubscriber.refresh(section); return attribute; } static async saveEventAttribute( attributes: IAttributeItem[] | any, buttonParentId: string = '', parentId: string = '', load: boolean = true, key: string[] = ['value'] ) { if (!attributes) return; const { section, type } = attributes[0]; attributes = await XM.Attribute.saveParams(parentId, attributes); const keyType = { style: 'styles', class: 'classes', util: 'utils', events: 'events', param: 'params', }; const safe = { parent: buttonParentId, key: (keyType)[type] || 'classes', access: 'attributes', }; const save = await InAppLookup.findAndReplaceArray(attributes, key, safe); if (load && section) SectionsSubscriber.refresh(section); return attributes; } }