import { extent } from "d3-array";
import { isUndefined } from "lodash-es";
import React from "react";
import { LayerColoringStrategyContext } from "../contexts/layer-coloring-strategy.context.js";
import {
  isSequentialLegend,
  isThresholdLegend
} from "../functions/build-scale-from-legend-config.js";
import { useColorScale } from "../hooks/use-color-scale.js";
import { useMapConfig } from "../hooks/use-map-config.js";
function buildCategoricalScale(scale) {
  return (value) => {
    const categories = scale.domain();
    if (categories.includes(value)) {
      return scale(value);
    }
  };
}
function buildNumericScale(scale) {
  return (value) => {
    const scaleDomain = scale.domain();
    const [min, max] = extent(scaleDomain);
    if (value && value >= min && value <= max) {
      return scale(value);
    }
  };
}
function buildClampedScale(legend, scale) {
  if (isSequentialLegend(legend)) {
    return buildNumericScale(scale);
  } else if (isThresholdLegend(legend)) {
    return buildNumericScale(scale);
  } else {
    return buildCategoricalScale(scale);
  }
}
const LayerColorStrategyProvider = ({ children }) => {
  const scale = useColorScale();
  const { legend } = useMapConfig();
  if (isUndefined(legend) || isUndefined(scale)) {
    return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
  }
  const clampedScale = buildClampedScale(legend, scale);
  return /* @__PURE__ */ React.createElement(LayerColoringStrategyContext.Provider, { value: clampedScale }, children);
};
export {
  LayerColorStrategyProvider,
  buildCategoricalScale,
  buildClampedScale,
  buildNumericScale
};
//# sourceMappingURL=layer-color-strategy.provider.js.map
