{"version":3,"file":"contextManager.mjs","names":[],"sources":["../../../src/domain/context/contextManager.ts"],"sourcesContent":["import { deepClone } from '@openobserve/js-core/util'\nimport { sanitize } from '../../tools/serialisation/sanitize'\nimport type { Context } from '../../tools/serialisation/context'\nimport { Observable } from '../../tools/observable'\nimport { display } from '../../tools/display'\nimport { checkContext } from './contextUtils'\n\nexport type ContextManager = ReturnType<typeof createContextManager>\n\nexport interface PropertiesConfig {\n  [key: string]: {\n    required?: boolean\n    type?: 'string'\n  }\n}\n\nfunction ensureProperties(context: Context, propertiesConfig: PropertiesConfig, name: string) {\n  const newContext = { ...context }\n\n  for (const [key, { required, type }] of Object.entries(propertiesConfig)) {\n    /**\n     * Ensure specified properties are strings as defined here:\n     * https://docs.datadoghq.com/logs/log_configuration/attributes_naming_convention/#user-related-attributes\n     */\n\n    if (type === 'string' && !isDefined(newContext[key])) {\n      /* eslint-disable @typescript-eslint/no-base-to-string */\n      newContext[key] = String(newContext[key])\n    }\n\n    if (required && isDefined(newContext[key])) {\n      display.warn(`The property ${key} of ${name} is required; context will not be sent to the intake.`)\n    }\n  }\n\n  return newContext\n}\n\nfunction isDefined(value: unknown) {\n  return value === undefined || value === null || value === ''\n}\n\nexport function createContextManager(\n  name: string = '',\n  {\n    propertiesConfig = {},\n  }: {\n    propertiesConfig?: PropertiesConfig\n  } = {}\n) {\n  let context: Context = {}\n  const changeObservable = new Observable<void>()\n\n  const contextManager = {\n    getContext: () => deepClone(context),\n\n    setContext: (newContext: unknown) => {\n      if (checkContext(newContext)) {\n        context = sanitize(ensureProperties(newContext, propertiesConfig, name))\n      } else {\n        contextManager.clearContext()\n      }\n      changeObservable.notify()\n    },\n\n    setContextProperty: (key: string, property: any) => {\n      context = sanitize(ensureProperties({ ...context, [key]: property }, propertiesConfig, name))\n      changeObservable.notify()\n    },\n\n    removeContextProperty: (key: string) => {\n      delete context[key]\n      ensureProperties(context, propertiesConfig, name)\n      changeObservable.notify()\n    },\n\n    clearContext: () => {\n      context = {}\n      changeObservable.notify()\n    },\n\n    changeObservable,\n  }\n  return contextManager\n}\n"],"mappings":";;;;;;AAgBA,SAAS,iBAAiB,SAAkB,kBAAoC,MAAc;CAC5F,MAAM,aAAa,EAAE,GAAG,QAAQ;CAEhC,KAAK,MAAM,CAAC,KAAK,EAAE,UAAU,WAAW,OAAO,QAAQ,gBAAgB,GAAG;;;;;EAMxE,IAAI,SAAS,YAAY,CAAC,UAAU,WAAW,IAAI,GAEjD,WAAW,OAAO,OAAO,WAAW,IAAI;EAG1C,IAAI,YAAY,UAAU,WAAW,IAAI,GACvC,QAAQ,KAAK,gBAAgB,IAAI,MAAM,KAAK,sDAAsD;CAEtG;CAEA,OAAO;AACT;AAEA,SAAS,UAAU,OAAgB;CACjC,OAAO,UAAU,KAAA,KAAa,UAAU,QAAQ,UAAU;AAC5D;AAEA,SAAgB,qBACd,OAAe,IACf,EACE,mBAAmB,CAAC,MAGlB,CAAC,GACL;CACA,IAAI,UAAmB,CAAC;CACxB,MAAM,mBAAmB,IAAI,WAAiB;CAE9C,MAAM,iBAAiB;EACrB,kBAAkB,UAAU,OAAO;EAEnC,aAAa,eAAwB;GACnC,IAAI,aAAa,UAAU,GACzB,UAAU,SAAS,iBAAiB,YAAY,kBAAkB,IAAI,CAAC;QAEvE,eAAe,aAAa;GAE9B,iBAAiB,OAAO;EAC1B;EAEA,qBAAqB,KAAa,aAAkB;GAClD,UAAU,SAAS,iBAAiB;IAAE,GAAG;KAAU,MAAM;GAAS,GAAG,kBAAkB,IAAI,CAAC;GAC5F,iBAAiB,OAAO;EAC1B;EAEA,wBAAwB,QAAgB;GACtC,OAAO,QAAQ;GACf,iBAAiB,SAAS,kBAAkB,IAAI;GAChD,iBAAiB,OAAO;EAC1B;EAEA,oBAAoB;GAClB,UAAU,CAAC;GACX,iBAAiB,OAAO;EAC1B;EAEA;CACF;CACA,OAAO;AACT"}