"use strict";

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

var _styledSystem = require("../theme/styled-system");

var _useTheme = require("./useTheme");

var _react = _interopRequireDefault(require("react"));

var _NativeBaseContext = require("../core/NativeBaseContext");

var _useResponsiveQuery = require("../utils/useResponsiveQuery");

var _stableHash = _interopRequireDefault(require("stable-hash"));

var _resolvePropsToStyle = require("./useThemeProps/resolvePropsToStyle");

var _reactNative = require("react-native");

var _tools = require("../theme/tools");

var _isEmptyObj = require("../utils/isEmptyObj");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/* eslint-disable react-hooks/exhaustive-deps */
//@ts-ignore
const getStyledSystemPropsAndRestProps = props => {
  const styledSystemProps = {};
  const restProps = {};
  const incomingAndThemeProps = (0, _tools.omitUndefined)(props);

  for (const key in incomingAndThemeProps) {
    if (key in _styledSystem.propConfig) {
      styledSystemProps[key] = incomingAndThemeProps[key];
    } else {
      restProps[key] = incomingAndThemeProps[key];
    }
  }

  return {
    styledSystemProps,
    restProps
  };
};

const useStyledSystemPropsResolver = ({
  style: propStyle,
  debug,
  ...props
}) => {
  const theme = (0, _useTheme.useTheme)();
  const {
    currentBreakpoint,
    config
  } = (0, _NativeBaseContext.useNativeBaseConfig)('makeStyledComponent');
  const strictMode = config.strictMode;
  const {
    getResponsiveStyles
  } = (0, _useResponsiveQuery.useResponsiveQuery)();
  const {
    styledSystemProps,
    restProps
  } = getStyledSystemPropsAndRestProps(props);

  const {
    style,
    dataSet
  } = _react.default.useMemo(() => {
    const resolvedStyle = (0, _resolvePropsToStyle.resolvePropsToStyle)(styledSystemProps, theme, _reactNative.Platform.OS, debug, currentBreakpoint, strictMode, getResponsiveStyles, restProps.INTERNAL_themeStyle, restProps.stateProps);
    return resolvedStyle; // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [// eslint-disable-next-line react-hooks/exhaustive-deps
  (0, _stableHash.default)(styledSystemProps), theme, currentBreakpoint, debug, strictMode, (0, _stableHash.default)(propStyle), getResponsiveStyles, (0, _stableHash.default)(props), (0, _stableHash.default)(restProps.stateProps)]); // if (process.env.NODE_ENV === "development" && debug) {
  //   /* eslint-disable-next-line */
  //   console.log("style,resprops", currentBreakpoint);
  // }
  // console.log('** use prop resolution 2', restProps);


  delete restProps.INTERNAL_themeStyle;
  restProps.dataSet = { ...restProps.dataSet,
    ...dataSet
  };

  const boxStyleSheet = _reactNative.StyleSheet.create({
    box: style
  }); // StyleSheet.create(style);


  let styleSheet;

  if (!(0, _isEmptyObj.isEmptyObj)(propStyle)) {
    styleSheet = [boxStyleSheet.box, propStyle];
  } else {
    styleSheet = boxStyleSheet.box;
  }

  return [styleSheet, restProps];
};

exports.useStyledSystemPropsResolver = useStyledSystemPropsResolver;
//# sourceMappingURL=useStyledSystemPropsResolver.js.map