"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMolecularComponentConfig = void 0;
var _typeHelpers = require("../theme/utils/type-helpers");
var _useTheme = require("./useTheme");
var _utils = require("./utils/utils");
var _useColorScheme = require("./useColorScheme");
var _useResponsiveProp = require("./useResponsiveProp");
var _useStyleProps = require("./useStyleProps");
var _styleFunctions = require("../theme/src/style-functions");
var _utils2 = require("../theme/utils/utils");
var _react = require("react");
var _lodash = _interopRequireDefault(require("lodash"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
 * useMolecularComponentConfig is a custom hook used to convert a molecular component style config to the appropriate React Native styles.
 * It takes the benefits of the useAtomicComponentConfig hook to the next level, allowing you to create complex components by combining different atomic components while still maintaining the ease of the styling through a component style config.
 *
 * @param themeComponentKey Name of the component in PearlTheme.components who's config needs to be used
 * @param receivedProps Raw props passed to the component where the hook is being used
 * @param sizeAndVariantProps Custom size and variant configuration to use
 * @param colorScheme Active color scheme of the component
 * @param styleFunctions List of style functions to use for computing the received style props
 * @param partForOverridenStyleProps The part where the style props passed to the component instance should be reflected. If undefined, the style props are passed to the first part as specified in the config
 * @param partForOverridenNativeProps The part where other native props (non-style props) passed to the component instance should be reflected. If undefined, the native props are passed to the first part as specified in the config
 * @param partForOverridenAnimationProps The part where animation props passed to the component instance should be reflected. If undefined, the animation props are passed to the first part as specified in the config
 * @returns
 */
const useMolecularComponentConfig = (componentName, receivedProps, sizeAndVariantProps = {
  size: undefined,
  variant: undefined
}, colorScheme = "primary", styleFunctions = _styleFunctions.boxStyleFunctions, partForOverridenStyleProps = undefined, partForOverridenNativeProps = undefined, partForOverridenAnimationProps = undefined) => {
  const {
    theme
  } = (0, _useTheme.useTheme)();
  (0, _utils.checkKeyAvailability)(componentName, theme.components, "theme.components");

  // User overriden props
  const buildStyleProperties = (0, _react.useMemo)(() => (0, _utils.composeStyleProps)(styleFunctions), [styleFunctions]);
  const overridenStyleProps = (0, _react.useMemo)(() => _lodash.default.pick(receivedProps, buildStyleProperties.properties), [receivedProps, buildStyleProperties]);
  const overridenAnimationProps = (0, _react.useMemo)(() => _lodash.default.pick(receivedProps, _utils.MOTI_PROPS), [receivedProps]);
  const overridenNativeProps = (0, _react.useMemo)(() => _lodash.default.omit(receivedProps, [..._utils.MOTI_PROPS, "style", ...buildStyleProperties.properties]), [receivedProps, buildStyleProperties]);
  const overridenProps = (0, _useStyleProps.useStyleProps)(receivedProps, styleFunctions);

  // Responsive Size and Variant
  const sizeForCurrentScreenSize = (0, _useResponsiveProp.useResponsiveProp)(sizeAndVariantProps.size);
  const variantForCurrentScreenSize = (0, _useResponsiveProp.useResponsiveProp)(sizeAndVariantProps.variant);
  const componentStyleConfig = (0, _react.useMemo)(() => theme.components[componentName], [theme, componentName]);
  const activeSizeAndVariantConfig = {};
  const defaultComponentConfig = (0, _react.useMemo)(() => componentStyleConfig["defaults"], [componentStyleConfig]);
  if (defaultComponentConfig) {
    if (defaultComponentConfig.hasOwnProperty("size")) {
      activeSizeAndVariantConfig.size = sizeForCurrentScreenSize ? sizeForCurrentScreenSize : defaultComponentConfig.size;
    }
    if (defaultComponentConfig.hasOwnProperty("variant")) {
      activeSizeAndVariantConfig.variant = variantForCurrentScreenSize ? variantForCurrentScreenSize : defaultComponentConfig.variant;
    }
  }
  if (!componentStyleConfig.hasOwnProperty("parts")) {
    throw new Error(`Key 'parts' does not exist in theme.components["${componentName}"]`);
  }
  let finalComponentProps = {};

  // Assign overriden props to the root
  if (!partForOverridenStyleProps) {
    finalComponentProps = {
      ...finalComponentProps,
      ...(0, _utils2.removeUndefined)(overridenStyleProps),
      style: receivedProps.style
    };
  }

  // Native props by default go to the 'root' part
  if (!partForOverridenNativeProps) {
    finalComponentProps = {
      ...finalComponentProps,
      ...(0, _utils2.removeUndefined)(overridenNativeProps)
    };
  }

  // Native props by default go to the 'root' part
  if (!partForOverridenAnimationProps) {
    finalComponentProps = {
      ...finalComponentProps,
      ...(0, _utils2.removeUndefined)(overridenAnimationProps)
    };
  }
  componentStyleConfig.parts.forEach(part => {
    const componentTypeStyles = (0, _typeHelpers.getKeys)(activeSizeAndVariantConfig).reduce((style, currProp) => {
      let activeSizeAndVariantStyles;
      if (currProp === "size") {
        (0, _utils.checkKeyAvailability)("sizes", componentStyleConfig, `theme.components['${String(componentName)}']`);
        (0, _utils.checkKeyAvailability)(activeSizeAndVariantConfig["size"], componentStyleConfig.sizes, `theme.components['${String(componentName)}']['sizes']`);
        let partConfig = componentStyleConfig.sizes[activeSizeAndVariantConfig[currProp]][part];
        if (typeof partConfig === "function") partConfig = partConfig(activeSizeAndVariantConfig.variant, colorScheme);
        activeSizeAndVariantStyles = partConfig;
      } else {
        (0, _utils.checkKeyAvailability)("variants", componentStyleConfig, `theme.components['${String(componentName)}']`);
        (0, _utils.checkKeyAvailability)(activeSizeAndVariantConfig["variant"], componentStyleConfig.variants, `theme.components['${String(componentName)}']['variants']`);
        let partConfig = componentStyleConfig.variants[activeSizeAndVariantConfig[currProp]][part];
        if (typeof partConfig === "function") partConfig = partConfig(activeSizeAndVariantConfig.size, colorScheme);
        activeSizeAndVariantStyles = partConfig;
      }
      if (style) {
        return {
          ...style,
          ...(0, _utils2.removeUndefined)(activeSizeAndVariantStyles)
        };
      }
      return activeSizeAndVariantStyles;
    }, null);
    let currentComponentPartProps = {
      ...((componentStyleConfig["baseStyle"] || {})[part] || {}),
      ...(0, _utils2.removeUndefined)(componentTypeStyles)
    };
    if (partForOverridenStyleProps && part === partForOverridenStyleProps) {
      currentComponentPartProps = {
        ...currentComponentPartProps,
        ...(0, _utils2.removeUndefined)(overridenStyleProps),
        style: {
          ...currentComponentPartProps.style,
          ...(0, _utils2.removeUndefined)(overridenProps.style)
        }
      };
    }
    if (partForOverridenNativeProps && part === partForOverridenNativeProps) {
      currentComponentPartProps = {
        ...currentComponentPartProps,
        ...(0, _utils2.removeUndefined)(overridenNativeProps)
      };
    }
    if (partForOverridenAnimationProps && part === partForOverridenAnimationProps) {
      currentComponentPartProps = {
        ...currentComponentPartProps,
        ...(0, _utils2.removeUndefined)(overridenAnimationProps)
      };
    }

    // For child references
    if (overridenProps.atoms && overridenProps.atoms[part]) currentComponentPartProps = {
      ...currentComponentPartProps,
      ...(0, _utils2.removeUndefined)(overridenProps.atoms[part])
    };
    finalComponentProps = {
      ...finalComponentProps,
      atoms: {
        ...finalComponentProps.atoms,
        [part]: currentComponentPartProps
      }
    };
  });
  finalComponentProps = (0, _useColorScheme.useColorScheme)(colorScheme, finalComponentProps);
  return finalComponentProps;
};
exports.useMolecularComponentConfig = useMolecularComponentConfig;
//# sourceMappingURL=useMolecularComponentConfig.js.map