'use client';
import { Pressable, StyleSheet, type GestureResponderEvent } from 'react-native';
import { useComboboxRootContext } from '../root/ComboboxRootContext';
import { useRenderElement } from '../../use-render/useRenderElement';
import { createChangeEventDetails } from '../../utils/createChangeEventDetails';
import { REASONS } from '../../utils/reasons';
import type { ZestUIComponentProps } from '../../types';
import { useStoreState } from '../../store/ReactStore';

/**
 * A full-screen surface that dismisses the list on an outside press.
 * Renders a `<Pressable>`.
 */
export function ComboboxBackdrop(componentProps: ComboboxBackdrop.Props) {
  const { render, className, style, ref, ...elementProps } = componentProps;

  const store = useComboboxRootContext();
  const open = useStoreState(store, 'open');

  const state: ComboboxBackdropState = { open };

  return useRenderElement(Pressable, componentProps, {
    state,
    ref,
    props: [
      {
        style: StyleSheet.absoluteFill,
        onPress(event: GestureResponderEvent) {
          if (store.select('disablePointerDismissal')) {
            return;
          }

          store.setOpen(false, createChangeEventDetails(REASONS.outsidePress, event));
          store.select('inputRef')?.current?.blur();
        },
      },
      elementProps,
    ],
  });
}

export interface ComboboxBackdropState {
  open: boolean;
}

export interface ComboboxBackdropProps
  extends ZestUIComponentProps<typeof Pressable, ComboboxBackdropState> {}

export namespace ComboboxBackdrop {
  export type State = ComboboxBackdropState;
  export type Props = ComboboxBackdropProps;
}
