"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const reactBinding = require("@contember/react-binding");
const ui = require("@contember/ui");
const react = require("react");
const DimensionsRenderer = require("./DimensionsRenderer.cjs");
const DimensionsStateRenderer = (props) => {
  if (props.accessorTreeState.name === "initialized") {
    return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: props.children });
  }
  return /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, {});
};
const DimensionsSwitcher = react.memo((props) => {
  const minItems = props.minItems === void 0 ? 1 : props.minItems;
  const maxItems = props.maxItems === void 0 ? 2 : props.maxItems;
  if (minItems > maxItems) {
    throw new reactBinding.BindingError(
      `DimensionSwitcher: 'minItems' for dimension ${props.dimension} must be no greater than 'maxItems'.`
    );
  }
  if (minItems < 1) {
    throw new reactBinding.BindingError(`DimensionSwitcher: 'minItems' for dimension ${props.dimension} must be at least 1.`);
  }
  const environment = reactBinding.useEnvironment();
  const qualifiedEntityList = reactBinding.QueryLanguage.desugarQualifiedEntityList(
    {
      orderBy: props.orderBy,
      entities: props.optionEntities
    },
    environment
  );
  const key = react.useMemo(() => JSON.stringify(qualifiedEntityList), [qualifiedEntityList]);
  const labelFactory = /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Field, { field: props.labelField });
  return /* @__PURE__ */ jsxRuntime.jsx(reactBinding.DataBindingProvider, { stateComponent: DimensionsStateRenderer, children: /* @__PURE__ */ jsxRuntime.jsxs(
    reactBinding.EntityListSubTree,
    {
      entities: qualifiedEntityList,
      orderBy: qualifiedEntityList.orderBy,
      offset: qualifiedEntityList.offset,
      limit: qualifiedEntityList.limit,
      listComponent: DimensionsRenderer.DimensionsRenderer,
      listProps: {
        buttonProps: props.buttonProps,
        dimension: props.dimension,
        labelFactory,
        minItems,
        maxItems,
        renderSelected: props.renderSelected,
        slugField: props.slugField
      },
      children: [
        labelFactory,
        /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Field, { field: props.slugField })
      ]
    }
  ) }, key);
});
DimensionsSwitcher.displayName = "DimensionsSwitcher";
exports.DimensionsSwitcher = DimensionsSwitcher;
//# sourceMappingURL=DimensionsSwitcher.cjs.map
