"use strict";

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

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

var _reactNativeSafeAreaContext = require("react-native-safe-area-context");

var _utils = require("@react-native-aria/utils");

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

var _HybridProvider = _interopRequireDefault(require("./hybrid-overlay/HybridProvider"));

var _overlays = require("@react-native-aria/overlays");

var _Toast = require("../components/composites/Toast");

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

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

var _utils2 = require("../theme/tools/utils");

var _platformSpecificSpaceUnits = require("../theme/tools/platformSpecificSpaceUnits");

var _styled = require("../utils/styled");

var _lodash = require("lodash");

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

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; }

(0, _styled.init)(); // For SSR to work, we need to pass initial insets as 0 values on web.
// https://github.com/th3rdwave/react-native-safe-area-context/issues/132

const defaultInitialWindowMetricsBasedOnPlatform = _reactNative.Platform.select({
  web: {
    frame: {
      x: 0,
      y: 0,
      width: 0,
      height: 0
    },
    insets: {
      top: 0,
      left: 0,
      right: 0,
      bottom: 0
    }
  },
  default: _reactNativeSafeAreaContext.initialWindowMetrics
});

const getComponentNamesFromTheme = theme => {
  const componentNames = theme !== null && theme !== void 0 && theme.components ? Object.keys(theme === null || theme === void 0 ? void 0 : theme.components) : [];
  return componentNames;
};

const getComponentVariants = componentTheme => {
  const variants = componentTheme.variants ? Object.keys(componentTheme.variants) : [];
  return variants;
};

const getComponentSizes = componentTheme => {
  const sizes = componentTheme.sizes ? Object.keys(componentTheme.sizes) : []; // console.log('component', componentTheme);

  return sizes;
};

function findCharacterFrequency(str, character) {
  let count = 0;

  for (let i = 0; i < str.length; i++) {
    if (str[i] === character) {
      count++;
    }
  }

  return count;
}

const isKeyWithSingleOrDoubleValue = key => {
  return findCharacterFrequency(key, '.') === 0 || findCharacterFrequency(key, '.') === 1 ? true : false;
};

const deleteFromResolvedMap = (object, providerId, value, condition) => {
  const keys = Object.keys(object === null || object === void 0 ? void 0 : object[providerId]);
  keys.forEach(key => {
    if (key.startsWith(value) && key.includes(condition)) {
      var _object$providerId;

      object === null || object === void 0 ? true : (_object$providerId = object[providerId]) === null || _object$providerId === void 0 ? true : delete _object$providerId[key];
    }

    if (key.startsWith(value) && !key.includes(condition) && isKeyWithSingleOrDoubleValue(key)) {
      var _object$providerId2;

      object === null || object === void 0 ? true : (_object$providerId2 = object[providerId]) === null || _object$providerId2 === void 0 ? true : delete _object$providerId2[key];
    }
  });
  return;
};

const NativeBaseProvider = props => {
  var _config$theme, _useWindowDimensions;

  const {
    colorModeManager,
    config = _NativeBaseContext.defaultConfig,
    children,
    theme: propsTheme = _theme.theme,
    initialWindowMetrics,
    isSSR,
    disableContrastText,
    providerId
  } = props; // @ts-ignore

  const {
    customUserTheme,
    ...theme
  } = (_config$theme = config.theme) !== null && _config$theme !== void 0 ? _config$theme : propsTheme;
  const ref = (0, _react.useRef)(providerId !== null && providerId !== void 0 ? providerId : (0, _lodash.uniqueId)('my-provider-'));
  (0, _react.useEffect)(() => {
    let components = getComponentNamesFromTheme(customUserTheme);
    components.map(component => {
      var _customUserTheme$comp, _customUserTheme$comp2;

      let variants = getComponentVariants(customUserTheme === null || customUserTheme === void 0 ? void 0 : (_customUserTheme$comp = customUserTheme.components) === null || _customUserTheme$comp === void 0 ? void 0 : _customUserTheme$comp[component]);
      let sizes = getComponentSizes(customUserTheme === null || customUserTheme === void 0 ? void 0 : (_customUserTheme$comp2 = customUserTheme.components) === null || _customUserTheme$comp2 === void 0 ? void 0 : _customUserTheme$comp2[component]);
      variants.map(variant => {
        deleteFromResolvedMap(_styled.resolvedStyledMap, // @ts-ignore
        ref.current, component, variant);
      });
      sizes.map(size => {
        // @ts-ignore
        deleteFromResolvedMap(_styled.resolvedStyledMap, ref.current, component, size);
      });
    });
  }, [customUserTheme, providerId]);

  const newTheme = _react.default.useMemo(() => {
    if (config.enableRem) {
      return (0, _platformSpecificSpaceUnits.platformSpecificSpaceUnits)(theme);
    }

    return theme;
  }, [config.enableRem, theme]);

  const windowWidth = (_useWindowDimensions = (0, _reactNative.useWindowDimensions)()) === null || _useWindowDimensions === void 0 ? void 0 : _useWindowDimensions.width;

  const currentBreakpoint = _react.default.useMemo(() => (0, _utils2.getClosestBreakpoint)(newTheme.breakpoints, windowWidth), [windowWidth, newTheme.breakpoints]);

  return /*#__PURE__*/_react.default.createElement(_NativeBaseContext.NativeBaseConfigProvider, {
    theme: newTheme,
    config: config,
    currentBreakpoint: currentBreakpoint,
    isSSR: isSSR,
    disableContrastText: disableContrastText,
    providerId: ref.current
  }, /*#__PURE__*/_react.default.createElement(_reactNativeSafeAreaContext.SafeAreaProvider, {
    initialMetrics: initialWindowMetrics !== null && initialWindowMetrics !== void 0 ? initialWindowMetrics : defaultInitialWindowMetricsBasedOnPlatform
  }, /*#__PURE__*/_react.default.createElement(_HybridProvider.default, {
    colorModeManager: colorModeManager,
    options: theme.config
  }, /*#__PURE__*/_react.default.createElement(_overlays.OverlayProvider, null, /*#__PURE__*/_react.default.createElement(_Toast.ToastProvider, null, /*#__PURE__*/_react.default.createElement(InitializeToastRef, null), /*#__PURE__*/_react.default.createElement(_utils.SSRProvider, null, children))))));
};

exports.NativeBaseProvider = NativeBaseProvider;

const InitializeToastRef = () => {
  const toast = (0, _Toast.useToast)();
  _Toast.ToastRef.current = toast;
  return null;
};
//# sourceMappingURL=NativeBaseProvider.js.map