import { ComponentType, ReactElement } from "react";
//#region src/types/attrs.d.ts
/** Callback form of `.attrs()` — receives current props and returns partial overrides. */
type AttrsCb<A> = (props: Partial<A>) => Partial<A>;
//#endregion
//#region src/types/utils.d.ts
type TObj = Record<string, unknown>;
type TFn = (...args: any) => any;
/**
 * A React component type that accepts additional static properties.
 * Supports both class/function components and forwardRef components.
 */
type ElementType<T extends TObj | unknown = any> = ComponentType<T> & Partial<Record<string, any>>;
/**
 * Forces TypeScript to expand/flatten a type for better IDE display.
 * Short-circuits for `any` — the mapped type would turn `any` into
 * `{ [x: string]: any; [x: number]: any; [x: symbol]: any }`, losing
 * its identity as `any` and breaking downstream type checks.
 */
type Id<T> = 0 extends 1 & T ? T : T extends (infer U) ? { [K in keyof U]: U[K]; } : never;
/**
 * Strips keys whose values are `never`, `null`, or `undefined`.
 * Uses tuple wrapping `[T[P]] extends [never]` to avoid distribution
 * over union types (a bare `T[P] extends never` would incorrectly
 * match union members).
 *
 * Short-circuits for `any` — the `as` clause in mapped types loses
 * index signatures, which would turn `any` into an empty type.
 */
type IsAny<T> = 0 extends 1 & T ? true : false;
type ExtractNullableKeys<T> = 0 extends 1 & T ? T : { [P in keyof T as IsAny<T[P]> extends true ? P : [T[P]] extends [never] ? never : [T[P]] extends [null | undefined] ? never : P]: T[P]; };
/** Merges two types: keeps all keys from L that don't exist in R, then adds all of R. */
type SpreadTwo<L, R> = Id<Pick<L, Exclude<keyof L, keyof R>> & R>;
/** Recursively spreads a tuple of types left-to-right. */
type Spread<A extends readonly [...any]> = A extends [infer L, ...infer R] ? SpreadTwo<L, Spread<R>> : unknown;
/** Recursively checks whether any element in the tuple is `any`. */
type _HasAny<A> = A extends [infer L, ...infer R] ? 0 extends 1 & L ? true : _HasAny<R> : false;
type MergeTypes<A extends readonly [...any]> = _HasAny<A> extends true ? any : ExtractNullableKeys<Spread<A>>;
/** Extracts the props type from a React component type (class or function). */
type ExtractProps<TComponentOrTProps> = TComponentOrTProps extends ComponentType<infer TProps> ? TProps : TComponentOrTProps;
//#endregion
//#region src/types/config.d.ts
/** A component that has been enhanced by attrs — identified by the `IS_ATTRS` marker. */
type AttrsComponentType = ElementType & {
  IS_ATTRS: true;
};
/** Parameters accepted by the `.config()` chaining method. */
type ConfigAttrs<C extends ElementType | unknown> = Partial<{
  name: string;
  component: C;
  /** No runtime effect in attrs: `data-attrs` is added in every non-production build regardless. Accepted for config-shape parity with rocketstyle, where `DEBUG` enables render logging. */
  DEBUG: boolean;
}>;
//#endregion
//#region src/types/hoc.d.ts
type GenericHoc = (component: ElementType) => ElementType;
/**
 * Parameters for `.compose()` — a record of named HOCs.
 * Setting a key to `null`, `undefined`, or `false` removes a
 * previously defined HOC from the chain.
 */
type ComposeParam = Record<string, GenericHoc | null | undefined | false>;
//#endregion
//#region src/types/AttrsComponent.d.ts
/**
 * @param OA   Origin component props params.
 * @param EA   Extended prop types
 * @param S    Defined statics
 * @param HOC  High-order components
 * @param DFP  Calculated final component props
 */
interface AttrsComponent<C extends ElementType = ElementType, OA extends TObj = {}, EA extends TObj = {}, S extends TObj = {}, HOC extends TObj = {}, DFP extends Record<string, any> = MergeTypes<[OA, EA]>> {
  (props: DFP & {
    ref?: any;
  }): ReactElement | null;
  /**
   * A chaining method to define default component theme
   * @param param  _object_
   *
   * ### Examples
   *
   * #### Component name / displayName
   * ```tsx
   * const base = attrsComponent
   *  .config({
   *    name: 'Component name'
   *  })
   * ```
   *
   * #### Replace component by a new one
   * ```tsx
   * const base = attrsComponent
   *  .config({
   *    component: (props) => <div {...props} />
   *  })
   * ```
   *
   * #### Component as provider
   * ```tsx
   * const parent = attrsComponent
   *  .config({
   *    provider: true
   *  })
   * ```
   *
   * #### Component as consumer
   * ```tsx
   * const base = attrsComponent
   *  .config({
   *    consumer: ctx => ctx<typeof parent>(({ pseudo, state, ...rest }) => ({
   *      pseudo,
   *      state
   *    }))
   *  })
   * ```
   *
   * #### Inversed theme
   * A possibility to set individually for each component to have `inversed` styles
   * when using dark / light theme modes
   * ```tsx
   * const base = attrsComponent
   *  .config({
   *    inversed: true
   *  })
   * ```
   *
   * #### Pass props to original component
   * A possibility to set individually for each component props names to be passed
   * to `origin` component
   *
   * ```tsx
   * const base = attrsComponent
   *  .config({
   *    passProps: ['disabled', 'readOnly']
   *  })
   * ```
   */
  config: <NC extends ElementType | unknown = unknown>({ name, component: NC, DEBUG }: ConfigAttrs<NC>) => NC extends ElementType ? AttrsComponent<NC, ExtractProps<NC>, EA, S, HOC> : AttrsComponent<C, OA, EA, S, HOC>;
  /**
   * A chaining method to define default component props
   * @param param   Can be either _object_ or a _callback_
   * @param config  Optional. Can define `priority` in chaining. All `attrs` callbacks
   *                having `priority` set to `true` will be calculated first
   *
   * #### Examples
   *
   * ##### Object as a parameter
   * ```tsx
   * const base = attrsComponent
   * const newElement = base.attrs({
   *  propA: 'value',
   *  propB: 'value,
   * })
   * ```
   *
   * ##### Callback as a parameter
   * ```tsx
   * const base = attrsComponent
   * const newElement = base.attrs((props, theme, helpers) => ({
   * propA: props.disabled ? 'valueA' : 'valueB',
   * propB: 'value,
   *  }))
   *  ```
   */
  attrs: <P extends TObj | unknown = unknown>(param: P extends TObj ? Partial<MergeTypes<[DFP, P]>> | AttrsCb<MergeTypes<[DFP, P]>> : Partial<DFP> | AttrsCb<DFP>, config?: Partial<{
    /**
     * priority props will be resolved first and overwritten by normal `attrs`
     * callbacks and `props` afterwards
     */
    priority: boolean;
    /**
     * filter props will be omitted when passing to final component
     */
    filter: unknown extends P ? string[] : (keyof MergeTypes<[EA, P]>)[];
  }>) => P extends TObj ? AttrsComponent<C, OA, MergeTypes<[EA, P]>, S, HOC> : AttrsComponent<C, OA, EA, S, HOC>;
  /**
   * A chaining method to define high-order components to wrap
   * the defined component
   * @param param object of key hoc function
   *
   * ### Examples
   *
   * #### Define new hoc component
   * Using the `{ key: hoc }` annotation allows to override or remove
   * the defined hoc(s) later on. See the examples below.
   * ```tsx
   * const hoc = (WrappedComponent) => (props) => <WrappedComponent {...props} />
   * const base = attrsComponent
   *
   * const newElement = base.compose({ hocName: hoc })
   * ```
   *
   * #### Remove previously defined hoc component
   * (1) Set value to be `false`
   * ```tsx
   * const newElement = base.compose({ hocName: false })
   * ```
   * (2) Set value to be `null`
   * ```tsx
   * const newElement = base.compose({ hocName: null })
   * ```
   * (3) Set value to be `undefined`
   * ```tsx
   * const newElement = base.compose({ hocName: undefined })
   * ```
   */
  compose: <P extends ComposeParam>(param: P) => P extends TObj ? AttrsComponent<C, OA, EA, S, MergeTypes<[HOC, P]>> : AttrsComponent<C, OA, EA, S, HOC>;
  /**
   * A chaining method to define statics on the attrs component.
   * All statics are accessible via `is` static key on the component.
   * @param param object of statics
   *
   * ### Examples
   *
   * #### Define new static
   * Using the `{ key: value }` annotation allows to override or remove
   * the defined static(s) later on. See the examples below.
   * ```tsx
   * const base = attrsComponent
   *
   * const newElement = base.statics({
   * isNewStatic: true,
   * arrayStatic: ['a', 'b'],
   * functionStatic: (param) => { ...do something }
   * })
   * ```
   *
   * #### Override previously defined hoc component
   * (1) Set value to be `false`
   * ```tsx
   * const newElement = base.statics({ isNewStatic: false })
   * ```
   * (2) Set value to be `null`
   * ```tsx
   * const newElement = base.statics({ isNewStatic: null })
   * ```
   * (3) Set value to be `undefined`
   * ```tsx
   * const newElement = base.statics({ isNewStatic: undefined })
   * ```
   */
  statics: <P extends TObj | unknown = unknown>(param: P) => P extends TObj ? AttrsComponent<C, OA, EA, MergeTypes<[S, P]>, HOC> : AttrsComponent<C, OA, EA, S, HOC>;
  /**
   * An access to all defined statics on the component.
   *
   * ### Examples
   *
   * #### Access a static property
   * ```tsx
   * const element = attrsComponent.statics({
   * isNewStatic: true,
   * arrayStatic: ['a', 'b'],
   * functionStatic: (param) => { ...do something }
   * })
   *
   * // staticValue = true
   * const staticValue = element.meta.isNewStatic
   * ```
   */
  meta: S;
  getDefaultAttrs: (props: TObj) => TObj;
  /**
   * Accessible via types only!
   *
   * Provides defined original props types (props of origin
   * component passed to _attrs_)
   *
   * ### Examples
   * ```tsx
   * const element = attrsComponent
   *
   * type Props = typeof element['$$originProps']
   *
   * ```
   */
  readonly $$originTypes: OA;
  /**
   * Accessible via types only!
   *
   * Provides defined extended props types (props types defined
   * on `.attrs()` chaining method
   * component passed to _attrs_)
   *
   * ### Examples
   * ```tsx
   * const element = attrsComponent
   *  .attrs<{ propName: string }>({})
   *
   * type Props = typeof element['$$extendedProps']
   *
   * ```
   */
  readonly $$extendedTypes: EA;
  /**
   * Accessible via types only!
   *
   * Provides all defined props types (including **origin**
   * props types & **extended** props types all together)
   *
   * ### Examples
   * ```tsx
   * const element = attrsComponent
   *
   * type Props = typeof element['$$allProps']
   *
   * ```
   */
  readonly $$types: DFP;
  /**
   * Static Attrs component identificator
   */
  IS_ATTRS: true;
  /**
   * Component displayName defined in `.config()` chaining
   * method
   *
   * ```tsx
   * const element = attrsComponent
   *  .config({
   *    name: 'ComponentName'
   *  })
   * ```
   */
  displayName: string;
}
//#endregion
//#region src/types/configuration.d.ts
type OptionFunc = (...arg: unknown[]) => Record<string, unknown>;
type InitConfiguration<C> = {
  name?: string;
  component: C;
};
/**
 * Internal configuration accumulated across the chaining API.
 * Arrays hold the full chain — each `.attrs()` call appends to these.
 */
type Configuration<C = ElementType | unknown> = InitConfiguration<C> & {
  /** No runtime effect in attrs: `data-attrs` is added in every non-production build regardless. Accepted for config-shape parity with rocketstyle, where `DEBUG` enables render logging. */
  DEBUG?: boolean;
  /** Chain of default-props callbacks (resolved in order, later wins). */
  attrs: OptionFunc[];
  /** Chain of priority-props callbacks (resolved before `attrs`, can be overridden by both). */
  priorityAttrs: OptionFunc[];
  /** Prop names to omit before passing to the underlying component. */
  filterAttrs: string[];
  /** Named HOCs — set to null/false to remove from chain. */
  compose: Record<string, TFn | null | undefined | false>;
  /** Metadata accessible via `Component.meta`. */
  statics: Record<string, any>;
} & Record<string, any>;
//#endregion
//#region src/types/InitAttrsComponent.d.ts
/**
 * Type of the internal `attrsComponent` factory function.
 * Takes a full Configuration and returns an AttrsComponent whose
 * original props (OA) are extracted from the component type C,
 * with all extension slots (EA, S, HOC) starting empty.
 */
type InitAttrsComponent<C extends ElementType = ElementType> = (params: Configuration<C>) => AttrsComponent<C, ExtractProps<C> // OA — original component props
, {} // EA — extended props (empty initially)
, {} // S  — statics (empty initially)
, {}>;
//#endregion
//#region src/init.d.ts
/**
 * Public entry point for creating an attrs-enhanced component.
 *
 * ```tsx
 * const Button = attrs({ name: 'Button', component: Element })
 *   .attrs({ tag: 'button' })
 *   .attrs<{ primary?: boolean }>(({ primary }) => ({
 *     backgroundColor: primary ? 'blue' : 'gray',
 *   }))
 * ```
 */
type Attrs = <C extends ElementType>({ name, component }: {
  name: string;
  component: C;
}) => ReturnType<InitAttrsComponent<C>>;
declare const attrs: Attrs;
//#endregion
//#region src/isAttrsComponent.d.ts
type IsAttrsComponent = <T>(component: T) => boolean;
/** Runtime type guard — checks if a component was created by `attrs()`. */
declare const isAttrsComponent: IsAttrsComponent;
//#endregion
export { type Attrs, type AttrsCb, type AttrsComponent, type AttrsComponentType, type ComposeParam, type ConfigAttrs, type ElementType, type GenericHoc, type IsAttrsComponent, type TObj, attrs, attrs as default, isAttrsComponent };