import React, { useContext, createContext } from 'react';

/**
 * The type of the PromoCard context object.
 *
 * The context object contains the current state of the PromoCard group, whether
 * the group is disabled or not, and a function to call when the group value
 * changes. This value is used to provide context to child PromoCard components,
 * allowing them to interact with the group and update its state.
 */
interface PromoCardContextType {
  /**
   * The current value of the PromoCard group.
   *
   * @default ''
   */
  state: string;
  /**
   * Whether the PromoCard group is disabled or not.
   *
   * @default false
   */
  isDisabled: boolean;
  /**
   * A function to call when the PromoCard group value changes.
   *
   * @param {string} value - The new value of the PromoCard group.
   * @default () => {}
   */
  onChange: (value: string) => void;
}

const defaultPromoCardContext: PromoCardContextType = {
  state: '',
  isDisabled: false,
  onChange: () => {},
};

/**
 * The PromoCard context object.
 */
export const PromoCardContext = createContext<PromoCardContextType>(defaultPromoCardContext);

/**
 * A custom hook for accessing the PromoCard context object.
 *
 * The `usePromoCardContext` hook is used to access the PromoCard context object
 * from within a child PromoCard component. It throws an error if the context
 * object is not available, which can help with debugging and development.
 *
 * @returns {PromoCardContextType} - The PromoCard context object.
 */
export const usePromoCardContext = (): PromoCardContextType => {
  return useContext(PromoCardContext);
};

export default PromoCardContext;
