"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.usePropsConfig = usePropsConfig;

var _react = require("react");

var _ThemeProvider = require("./../ThemeProvider");

var _lodash = require("lodash");

var _base = require("./../base");

var _utils = require("./utils");

function usePropsConfig(component, props) {
  const theme = (0, _react.useContext)(_ThemeProvider.ThemeContext).theme;

  if (!props) {
    props = {};
  }

  const componentTheme = (0, _lodash.get)(theme, "components.".concat(component));

  if (!componentTheme) {
    console.warn("NB Warning: If you are seeing this, you probably don't need to use usePropsConfig in ".concat(component, "."));
    return props;
  }

  props = (0, _utils.omitUndefined)(props); // Extracting props from defaultProps

  let newProps = extractProps(componentTheme.defaultProps, theme, componentTheme); // Extracting props from base style

  let componentBaseStyle = typeof componentTheme.baseStyle !== 'function' ? componentTheme.baseStyle : componentTheme.baseStyle({
    theme,
    ...newProps,
    ...props
  });
  newProps = (0, _lodash.mergeWith)(newProps, componentBaseStyle, // @ts-ignore
  (objValue, srcValue, key) => {
    if (!(0, _lodash.isNil)(objValue)) {
      delete newProps[key];
    }
  }); // Extracting props from normal props

  let extractedProps = extractProps(props, theme, componentTheme); // added this to handle order of props
  // @ts-ignore

  newProps = (0, _lodash.mergeWith)(newProps, extractedProps, (objValue, srcValue, key) => {
    if (!(0, _lodash.isNil)(objValue)) {
      delete newProps[key];
    }
  }); // Extracting props from variant

  if (componentTheme.variants && newProps.variant && componentTheme.variants[newProps.variant]) {
    const colorScheme = newProps.colorScheme || componentTheme.defaultProps.colorScheme;
    let variantProps = componentTheme.variants[newProps.variant]({
      colorScheme,
      theme
    }); // @ts-ignore

    newProps = (0, _lodash.mergeWith)(newProps, variantProps, (objValue, srcValue, key) => {
      if (!(0, _lodash.isNil)(objValue)) {
        delete newProps[key];
      }
    });
    delete newProps.variant;
    delete newProps.colorScheme;
  }

  newProps = (0, _utils.omitUndefined)(newProps);
  return newProps;
}
/*
 Extract props from theme props and omit those from props
*/


function extractProps(props, theme, componentTheme) {
  let newProps = {};

  for (let property in props) {
    // If the property exists in theme map then get its value
    if (_base.themePropertyMap[property]) {
      let propValues = (0, _lodash.get)(componentTheme, "".concat(_base.themePropertyMap[property], ".").concat(props[property]));

      if (typeof propValues === 'string' || typeof propValues === 'number') {
        newProps[property] = propValues;
      } else if (!(0, _lodash.isNil)(propValues)) {
        for (let nestedProp in propValues) {
          newProps[nestedProp] = (0, _lodash.get)(theme, "".concat(_base.themePropertyMap[nestedProp], ".").concat(propValues[nestedProp]), propValues[nestedProp]);
        }
      } else if (property === 'shadow') {
        let shadowProps = (0, _lodash.get)(theme, "".concat(_base.themePropertyMap[property], ".").concat(props[property]));

        if (!(0, _lodash.isNil)(shadowProps)) {
          newProps = { ...newProps,
            ...shadowProps
          };
        }
      } else {
        newProps[property] = props[property];
      }
    } else {
      newProps[property] = props[property];
    }
  }

  return newProps;
}
//# sourceMappingURL=usePropsConfig.js.map