"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useMotiWithStyleProps = void 0;
var _react = require("react");
var _typeHelpers = require("../theme/utils/type-helpers");
var _useTheme = require("./useTheme");
var _utils = require("./utils/utils");
var _styleProperties = require("../theme/src/style-properties");
var _lodash = _interopRequireDefault(require("lodash"));
var _utils2 = require("../theme/utils/utils");
var _reactNative = require("react-native");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const shorthandPropMapper = {
  ..._styleProperties.layoutPropertiesShorthand,
  ..._styleProperties.spacingPropertiesShorthand
};

/**
 * Converts Moti Props using style props as well
 * @param props - The props to convert
 * @param styleFunctions - The style functions to use
 * @returns The converted props
 */
const useMotiWithStyleProps = (props, styleFunctions) => {
  const {
    theme,
    colorMode
  } = (0, _useTheme.useTheme)();
  const dimensions = (0, _reactNative.useWindowDimensions)();

  // Compose style properties
  const buildStyleProperties = (0, _react.useMemo)(() => (0, _utils.composeStyleProps)(styleFunctions), [styleFunctions]);
  const nonAnimateableStyleProps = ["flex", "flexShrink", "flexGrow", "flexBasis", "flexDirection", "flexWrap", "shadowOffset", "textShadowOffset", "fontFamily", "fontWeight", "fontSize", "lineHeight", "fontStyle", "textAlign", "letterSpacing", "textDecorationLine", "textDecorationStyle", "textTransform"];
  const animateableStyles = _lodash.default.omit(props.style, nonAnimateableStyleProps);

  // Convert 'from', 'animate', 'transition', 'exit', 'exitTransition' props
  ["from", "animate", "transition", "exit", "exitTransition"].forEach(prop => {
    if (prop === "animate" && props[prop] === undefined) props[prop] = {};
    if (props[prop]) {
      props[prop] = (0, _utils.composeCleanStyleProps)(props[prop], buildStyleProperties, {
        theme,
        colorMode,
        dimensions
      });
      if (prop === "animate") {
        // Merge animateableStyles and animate
        props.animate = {
          ...animateableStyles,
          ...(0, _utils2.removeUndefined)(props.animate)
        };
      }
      if (["transition", "exitTransition"].includes(prop)) {
        // Filter object values from 'transition' or 'exitTransition'
        const keysWithObjectValues = (0, _typeHelpers.getKeys)(props[prop]).filter(key => typeof props[prop][key] === "object");
        const propsWithObjectValues = _lodash.default.pick(props[prop], keysWithObjectValues);
        const nullObject = keysWithObjectValues.reduce((obj, key) => {
          return {
            ...obj,
            [key]: null
          };
        }, {});
        props[prop] = {
          ...props[prop],
          ...(0, _utils2.removeUndefined)(nullObject)
        };

        // Add the filtered values back into 'transition' or 'exitTransition'
        props[prop] = keysWithObjectValues.reduce((obj, key) => {
          let finalKey = key;
          if ((0, _typeHelpers.getKeys)(shorthandPropMapper).includes(key)) {
            finalKey = shorthandPropMapper[key];
          }
          return {
            ...obj,
            [finalKey]: propsWithObjectValues[key]
          };
        }, props[prop]);
      }
    }
  });

  // props.style = _.pick(props.style, nonAnimateableStyleProps);
  return props;
};
exports.useMotiWithStyleProps = useMotiWithStyleProps;
//# sourceMappingURL=useMotiWithStyleProps.js.map