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A;AAAA,MACH;AAAA,KACD,CAAA;AAAA,IACD;AAAA,GACF;AACF","file":"index.cjs","sourcesContent":["import { Box, type BoxContext } from '@bento/box';\nimport { useInternalProps } from '@bento/internal-props';\nimport { AnyObject } from '@bento/types';\nimport { useContext } from 'react';\n\nexport interface RenderPropData {\n  /**\n   * If the component is assigning a default value to the given prop,\n   * the original value be a reference to the previous assigned value.\n   * @default The original assigned value\n   */\n  original?: unknown;\n\n  /** All the props that were passed to the component. @default {} */\n  props: AnyObject;\n\n  /** If slots are used to modify the component, this will contain a reference to the original slots object. @default {} */\n  slots: AnyObject;\n\n  /** The exposed state of the component. @default {} */\n  state: AnyObject;\n}\n\n/**\n * Checks if the given string is an event listener name. An event listener name\n * is defined as a string that starts with \"on\" followed by an uppercase letter.\n *\n * @param name - The string to check.\n * @returns `true` if the string is an event listener name, otherwise `false`.\n * @private\n */\nexport function isEventListener(name: string): boolean {\n  return /^on[A-Z]/.test(name);\n}\n\n/**\n * Determines if a given value is a render prop.\n *\n * A render prop is a function that is used to dynamically generate\n * UI elements. This function checks if the provided value is a function\n * and not an event listener.\n *\n * @param name - The name of the prop.\n * @param value - The value of the prop to check.\n * @returns `true` if the value is a render prop, otherwise `false`.\n */\nexport function isRenderProp(name: string, value: any): boolean {\n  return typeof value === 'function' && !isEventListener(name);\n}\n\n/**\n * Executes a function or returns a value from the given data object.\n *\n * @param name - The name of the property to execute or retrieve.\n * @param data - The data object containing the property.\n * @param args - The arguments to pass if the property is a function.\n * @returns The result of the function execution or the value of the property.\n * @private\n */\nexport function execute(name: string, data: AnyObject, args: RenderPropData): any {\n  const value = data[name];\n\n  if (isRenderProp(name, value)) return value(args);\n  return value;\n}\n\n/**\n * Retrieves a property value, potentially overridden by a slotted value.\n *\n * @param name - The name of the property to retrieve.\n * @param options.props - The original properties.\n * @param options.slots - The slotted values that can override the original properties.\n * @param options.state - The current state.\n * @param options.original - The original value of the property.\n * @returns The original property value or the overridden value if provided.\n */\nexport function renderProp(name: string, args: RenderPropData): any {\n  const { props, slots, original } = args;\n\n  return execute(name, slots, args) || execute(name, props, args) || original;\n}\n\nexport interface Returns {\n  /**\n   * Proxy object that have access to the original props, slotted values, and internal props. When\n   * accessing a property, it will first check the slotted values, then the original props, and finally\n   * the internal props. If the property is a render prop, it will execute the function with the provided\n   * arguments.\n   *\n   * @default { ...props, ...slots }\n   */\n  props: AnyObject;\n  /**\n   * Applies the given attributes as default values to the props. If no attributes are provided, it will\n   * use the props as default values. The resulting object will contain all the properties of the props,\n   * except for the ones specified in the `except` array. The values of the properties will be the result\n   * of executing the render prop function with the provided arguments.\n   *\n   * @default function(attr)\n   */\n  apply: (attributes?: object, except?: string[]) => object;\n}\n\n/**\n * Hook that merges props with slotted props and provides a proxy for accessing them.\n *\n * @param args - The initial props to use.\n * @param state - The state object to use.\n * @returns An object containing the proxy based props object and the apply function.\n * @throws {BentoError} If the hook is used outside of a @bento/slots component.\n *\n * @example\n * const { props, apply } = useProps({ foo: 'bar' });\n * if (props.a) doSomething()\n * return <a {...apply({ className: 'foo' }) }>{ props.children }</a>;\n */\nexport function useProps(args: AnyObject, state: object = {}): Returns {\n  const { slots } = useContext<BoxContext<AnyObject>>(Box);\n  const [props, internal] = useInternalProps(args);\n  const { namespace, assigned } = slots;\n  const dot = namespace.join('.');\n  const slotted = assigned[dot] || {};\n  const propsy = { ...internal, ...props, ...slotted };\n\n  /**\n   * Applies the given attributes to an object.\n   *\n   * @param attributes - The attributes to apply. If not provided, defaults to `propsy`.\n   * @param except - An array of keys to exclude from the resulting object.\n   * @returns The resulting object with applied attributes.\n   * @public\n   */\n  function apply(attributes?: object, except?: string[]): object {\n    const data = attributes || propsy;\n    const returned = {};\n\n    function reduce(memo: AnyObject, key: string) {\n      if (except && except.includes(key)) return memo;\n\n      memo[key] = renderProp(key, {\n        original: data[key],\n        slots: slotted,\n        props: { ...props, ...internal },\n        state\n      });\n\n      return memo;\n    }\n\n    if (!attributes) return Object.keys(propsy).reduce(reduce, returned);\n    return Object.keys(propsy).reduce(reduce, Object.keys(attributes).reduce(reduce, returned));\n  }\n\n  return {\n    props: new Proxy(propsy, {\n      get: function getter(_: object, name: string) {\n        return renderProp(name, {\n          original: isRenderProp(name, props[name]) ? undefined : props[name],\n          slots: slotted,\n          props: { ...props, ...internal },\n          state\n        });\n      }\n    }),\n    apply\n  };\n}\n"]}