{"version":3,"file":"value.cjs","names":["getByKey"],"sources":["../../../../src/guards/functions/helpers/value.ts"],"sourcesContent":["import type { PrimitiveObject } from '#bemedev/globals/types';\nimport type { EventObject } from '#events';\nimport { getByKey } from '#machines';\nimport type { FnR } from '~types';\nimport type { DefinedValue } from '../../types';\n\nexport type IsValueS_F = <\n  E extends EventObject = EventObject,\n  Pc = any,\n  Tc extends PrimitiveObject = PrimitiveObject,\n  T extends string = string,\n>(\n  path: DefinedValue<Pc, Tc>,\n  ...values: any[]\n) => FnR<E, Pc, Tc, T, boolean>;\n\n/**\n * Checks if the value at the specified path in pContext, context, or events matches any of the provided values.\n * @param path of type {@linkcode DefinedValue}, the path to retrieve.\n * @enum\n * The path can be one of the following:\n * - 'pContext': checks if the value in pContext is one of the provided values\n * - 'context': checks if the value in context is one of the provided values\n * - 'events': checks if the value in events is one of the provided values\n * - 'context.[key]': checks if the value in context at the specified key is one of the provided values\n * - 'pContext.[key]': checks if the value in pContext at the specified key is one of the provided values\n * - 'events.[key]': checks if the value in events at the specified key is one of the provided values\n * @param values the values to check against.\n * @returns a {@linkcode FnR} function that takes pContext, context, and eventsMap and returns true if the value at the specified path matches any of the provided values, false otherwise.\n *\n * @example\n * ```ts\n * const guard = isValue('context.userId', '123', '456');\n * const result = guard({ context: { userId: '123' } }, {}, {});\n * console.log(result); // true\n * ```\n *\n * @see {@linkcode EventObject} for the type of the events map.\n * @see {@linkcode PromiseeMap} for the type of the promisees map.\n * @see {@linkcode PrimitiveObject} for the type of the context.\n * @see {@linkcode getByKey} for retrieving values by key.\n *  @see {@linkcode t} for type checking and validation.\n *\n * @see {@linkcode isNotValue} for the opposite check.\n */\nexport const isValue: IsValueS_F = (path, ...values) => {\n  const start = path.startsWith.bind(path);\n\n  return ({ pContext, context, event }) => {\n    if (path === 'pContext') {\n      return values.some(value => pContext === value);\n    }\n\n    if (path === 'context') {\n      return values.some(value => context === value);\n    }\n\n    if (path === 'events') return values.some(value => event === value);\n\n    if (start('context.')) {\n      const key = path.replace('context.', '');\n      return values.some(value => getByKey(context, key) === value);\n    }\n\n    if (start('pContext.')) {\n      const key = path.replace('pContext.', '');\n      return values.some(value => getByKey(pContext, key) === value);\n    }\n\n    const key = path.replace('events.', '');\n    const toValidate = getByKey(event, key);\n    return values.some(value => toValidate === value);\n  };\n};\n\n/**\n * Checks if the value at the specified path in pContext, context, or events doesn't matches any of the provided values.\n * @param path of type {@linkcode DefinedValue}, the path to retrieve.\n * @enum\n * The path can be one of the following:\n * - 'pContext': checks if the value in pContext is one of the provided values\n * - 'context': checks if the value in context is one of the provided values\n * - 'events': checks if the value in events is one of the provided values\n * - 'context.[key]': checks if the value in context at the specified key is one of the provided values\n * - 'pContext.[key]': checks if the value in pContext at the specified key is one of the provided values\n * - 'events.[key]': checks if the value in events at the specified key is one of the provided values\n * @param values the values to check against.\n * @returns a {@linkcode FnR} function that takes pContext, context, and eventsMap and returns true if the value at the specified path matches doesn't any of the provided values, false otherwise.\n *\n * @example\n * ```ts\n * const guard = isNotValue('context.userId', '123', '456');\n * const result = guard({ context: { userId: '123' } }, {}, {});\n * console.log(result); // false\n * ```\n *\n * @see {@linkcode EventObject} for the type of the events map.\n * @see {@linkcode PromiseeMap} for the type of the promisees map.\n * @see {@linkcode PrimitiveObject} for the type of the context.\n * @see {@linkcode getByKey} for retrieving values by key.\n * @see {@linkcode t} for type checking and validation.\n *\n * @see {@linkcode isValue} for the opposite check.\n */\nexport const isNotValue: IsValueS_F = (path, ...values) => {\n  const func = isValue(path, ...values);\n\n  return state => {\n    const result = func(state as any);\n    return !result;\n  };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,MAAa,WAAuB,MAAM,GAAG,WAAW;CACtD,MAAM,QAAQ,KAAK,WAAW,KAAK,KAAK;AAExC,SAAQ,EAAE,UAAU,SAAS,YAAY;AACvC,MAAI,SAAS,WACX,QAAO,OAAO,MAAK,UAAS,aAAa,MAAM;AAGjD,MAAI,SAAS,UACX,QAAO,OAAO,MAAK,UAAS,YAAY,MAAM;AAGhD,MAAI,SAAS,SAAU,QAAO,OAAO,MAAK,UAAS,UAAU,MAAM;AAEnE,MAAI,MAAM,WAAW,EAAE;GACrB,MAAM,MAAM,KAAK,QAAQ,YAAY,GAAG;AACxC,UAAO,OAAO,MAAK,UAASA,6CAAAA,SAAS,SAAS,IAAI,KAAK,MAAM;;AAG/D,MAAI,MAAM,YAAY,EAAE;GACtB,MAAM,MAAM,KAAK,QAAQ,aAAa,GAAG;AACzC,UAAO,OAAO,MAAK,UAASA,6CAAAA,SAAS,UAAU,IAAI,KAAK,MAAM;;EAIhE,MAAM,aAAaA,6CAAAA,SAAS,OADhB,KAAK,QAAQ,WAAW,GAAG,CACA;AACvC,SAAO,OAAO,MAAK,UAAS,eAAe,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCrD,MAAa,cAA0B,MAAM,GAAG,WAAW;CACzD,MAAM,OAAO,QAAQ,MAAM,GAAG,OAAO;AAErC,SAAO,UAAS;AAEd,SAAO,CADQ,KAAK,MAAa"}