"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.unstable_resetCreateSelectorCache = exports.createSelector = void 0;
var _reselect = require("reselect");
var _warning = require("./warning");
const cacheContainer = {
  cache: new WeakMap()
};
const missingInstanceIdWarning = (0, _warning.buildWarning)(['MUI: A selector was called without passing the instance ID, which may impact the performance of the grid.', 'To fix, call it with `apiRef`, e.g. `mySelector(apiRef)`, or pass the instance ID explicitly, e.g `mySelector(state, apiRef.current.instanceId)`.']);
const createSelector = (...args) => {
  const selector = (...selectorArgs) => {
    const [stateOrApiRef, instanceId] = selectorArgs;
    const isApiRef = !!stateOrApiRef.current;
    const cacheKey = isApiRef ? stateOrApiRef.current.instanceId : instanceId ?? {
      id: 'default'
    };
    const state = isApiRef ? stateOrApiRef.current.state : stateOrApiRef;
    if (process.env.NODE_ENV !== 'production') {
      if (cacheKey.id === 'default') {
        missingInstanceIdWarning();
      }
    }
    const {
      cache
    } = cacheContainer;
    if (cache.get(cacheKey) && cache.get(cacheKey)?.get(args)) {
      // We pass the cache key because the called selector might have as
      // dependency another selector created with this `createSelector`.
      return cache.get(cacheKey)?.get(args)(state, cacheKey);
    }
    const newSelector = (0, _reselect.createSelector)(...args);
    if (!cache.get(cacheKey)) {
      cache.set(cacheKey, new Map());
    }
    cache.get(cacheKey)?.set(args, newSelector);
    return newSelector(state, cacheKey);
  };

  // We use this property to detect if the selector was created with createSelector
  // or it's only a simple function the receives the state and returns part of it.
  selector.acceptsApiRef = true;
  return selector;
};

// eslint-disable-next-line @typescript-eslint/naming-convention
exports.createSelector = createSelector;
const unstable_resetCreateSelectorCache = () => {
  cacheContainer.cache = new WeakMap();
};
exports.unstable_resetCreateSelectorCache = unstable_resetCreateSelectorCache;