import isPropValid from '@emotion/is-prop-valid';
import { all as allProps } from './config';
const allPropnames = ['mode', 'variant', ...Object.keys(allProps)];

/**
 * Emotion will not attempt to forward all system props - so this pre filters all possible exceptions to search agains
 * props like `color` and `width`.
 */
const validPropnames = allPropnames.filter(isPropValid);
export function createStyledOptions(additional = []) {
  // Cache possible valid prop names to prevent searching a larger list.
  const additionalExclusions = additional.filter(isPropValid);
  return {
    shouldForwardProp: prop => isPropValid(prop) && !validPropnames.includes(prop) && !additionalExclusions.includes(prop)
  };
}

/** Return type of createStyledOptions */

/** Type for styledOptions - callable with generics AND has shouldForwardProp property */

/**
 * @description
 * This object can be passed to the second argument of `styled('div', styledOptions)` or be called as a function to filter additional prop names
 * If you are extending a component that already has filtered props - you do not need to provide additional guards if  you are not passing additional props
 * @example
 * import { styledOptions } from '@codecademy/gamut-styles';
 * // default case for divs
 * styled('div', styledOptions)();
 * // elements other than div
 * styled('svg', styledOptions<'svg'>())();
 * // additional non system props to filter
 * styled('div', styledOptions(['data-invalidsomething']))();
 * // Extending existing components
 * styled(Box)()
 *
 */
export const styledOptions = Object.assign(createStyledOptions, createStyledOptions());