"use strict";

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

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

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

var _useThemeProps = require("../../../hooks/useThemeProps");

var _theme = require("../../../theme");

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

var _makeStyledComponent = require("../../../utils/makeStyledComponent");

var _wrapStringChild = require("../../../utils/wrapStringChild");

var _useSafeArea = require("../../../hooks/useSafeArea");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const StyledBox = (0, _makeStyledComponent.makeStyledComponent)(_reactNative.View);
let MemoizedGradient;

const safeAreaPropsExists = props => {
  if (props.safeArea || props.safeAreaX || props.safeAreaY || props.safeAreaTop || props.safeAreaBottom || props.safeAreaLeft || props.safeAreaRight) {
    return true;
  }

  return false;
};

const gradientPropExists = props => {
  var _props$bg, _props$background, _props$bgColor, _props$backgroundColo;

  if ((_props$bg = props.bg) !== null && _props$bg !== void 0 && _props$bg.linearGradient || (_props$background = props.background) !== null && _props$background !== void 0 && _props$background.linearGradient || (_props$bgColor = props.bgColor) !== null && _props$bgColor !== void 0 && _props$bgColor.linearGradient || (_props$backgroundColo = props.backgroundColor) !== null && _props$backgroundColo !== void 0 && _props$backgroundColo.linearGradient) {
    return true;
  }

  return false;
};

const GradientBox = /*#__PURE__*/(0, _react.forwardRef)(({
  children,
  _text,
  ...props
}, ref) => {
  var _props$bg2, _props$background2, _props$bgColor2, _props$backgroundColo2, _lgrad$colors, _useNativeBaseConfig$;

  const theme = (0, _useTheme.useTheme)(); // return null;

  const lgrad = ((_props$bg2 = props.bg) === null || _props$bg2 === void 0 ? void 0 : _props$bg2.linearGradient) || ((_props$background2 = props.background) === null || _props$background2 === void 0 ? void 0 : _props$background2.linearGradient) || ((_props$bgColor2 = props.bgColor) === null || _props$bgColor2 === void 0 ? void 0 : _props$bgColor2.linearGradient) || ((_props$backgroundColo2 = props.backgroundColor) === null || _props$backgroundColo2 === void 0 ? void 0 : _props$backgroundColo2.linearGradient);
  lgrad.colors = (_lgrad$colors = lgrad.colors) === null || _lgrad$colors === void 0 ? void 0 : _lgrad$colors.map(color => {
    return (0, _theme.getColor)(color, theme.colors, theme);
  });
  let startObj = {
    x: 0,
    y: 0
  };
  let endObj = {
    x: 0,
    y: 1
  };

  if (lgrad.start && lgrad.start.length === 2) {
    startObj = {
      x: lgrad.start[0],
      y: lgrad.start[1]
    };
  }

  if (lgrad.end && lgrad.end.length === 2) {
    endObj = {
      x: lgrad.end[0],
      y: lgrad.end[1]
    };
  }

  let Gradient = (_useNativeBaseConfig$ = (0, _NativeBaseContext.useNativeBaseConfig)('NativeBaseConfigProvider').config.dependencies) === null || _useNativeBaseConfig$ === void 0 ? void 0 : _useNativeBaseConfig$['linear-gradient'];

  if (!MemoizedGradient) {
    MemoizedGradient = (0, _makeStyledComponent.makeStyledComponent)(Gradient);
  } // const backgroundColorProps = [
  //   'bg',
  //   'bgColor',
  //   'background',
  //   'backgroundColor',
  // ];
  // backgroundColorProps.forEach((backgroundColorProp) => {
  //   if (backgroundColorProp in safeAreaProps)
  //     delete safeAreaProps[backgroundColorProp];
  // });
  // return 'hello';


  return /*#__PURE__*/_react.default.createElement(MemoizedGradient, _extends({
    ref: ref
  }, props, {
    colors: lgrad.colors,
    start: startObj,
    end: endObj,
    locations: lgrad.locations
  }), (0, _wrapStringChild.wrapStringChild)(children, _text));
}); // const getContext = () => {
//   // const context = React.useContext(Context);
//   // return context;
//   console.log(window['currentBreakpoint'], 'hello current breakpoint');
// };

const Box = ({
  children,
  ...props
}, ref) => {
  const {
    _text,
    ...resolvedProps
  } = (0, _useThemeProps.usePropsResolution)('Box', props); // console.log(resolvedProps, props, 'resolved props here');

  if (safeAreaPropsExists(props)) {
    return /*#__PURE__*/_react.default.createElement(BoxComponentWithSafeArea, _extends({}, resolvedProps, {
      _text: _text,
      ref: ref
    }), children);
  } else {
    return /*#__PURE__*/_react.default.createElement(BoxComponentWithoutSafeArea, _extends({}, resolvedProps, {
      _text: _text,
      ref: ref
    }), children);
  } // return (
  //   <StyledBox ref={ref} {...safeAreaProps}>
  //     {/* {React.Children.map(children, (child) => {
  //       return typeof child === 'string' ||
  //         typeof child === 'number' ||
  //         (child?.type === React.Fragment &&
  //           (typeof child.props?.children === 'string' ||
  //             typeof child.props?.children === 'number')) ? (
  //         <Text {..._text}>{child}</Text>
  //       ) : (
  //         child
  //       );
  //     })} */}
  //     {wrapStringChild(children, _text)}
  //   </StyledBox>
  // );

};

const BoxComponentWithSafeArea = /*#__PURE__*/(0, _react.forwardRef)(({
  children,
  _text,
  ...props
}, ref) => {
  const safeAreaProps = (0, _useSafeArea.useSafeArea)(props);

  if (gradientPropExists(props) && GradientBox) {
    return /*#__PURE__*/_react.default.createElement(GradientBox, _extends({}, props, safeAreaProps, {
      _text: _text,
      ref: ref
    }), children);
  } else {
    return /*#__PURE__*/_react.default.createElement(StyledBox, _extends({
      ref: ref
    }, safeAreaProps), (0, _wrapStringChild.wrapStringChild)(children, _text));
  }
});
const BoxComponentWithoutSafeArea = /*#__PURE__*/(0, _react.forwardRef)(({
  children,
  _text,
  ...props
}, ref) => {
  if (gradientPropExists(props) && GradientBox) {
    return /*#__PURE__*/_react.default.createElement(GradientBox, _extends({}, props, {
      ref: ref,
      _text: _text
    }), children);
  } else {
    return /*#__PURE__*/_react.default.createElement(StyledBox, _extends({
      ref: ref
    }, props), (0, _wrapStringChild.wrapStringChild)(children, _text));
  }
});

var _default = /*#__PURE__*/(0, _react.memo)( /*#__PURE__*/(0, _react.forwardRef)(Box));

exports.default = _default;
//# sourceMappingURL=index.js.map