import { jsxDEV, Fragment } from "react/jsx-dev-runtime";
import { BindingError, useEnvironment, QueryLanguage, Field, DataBindingProvider, EntityListSubTree } from "@contember/react-binding";
import { Spinner } from "@contember/ui";
import { memo, useMemo } from "react";
import { DimensionsRenderer } from "./DimensionsRenderer.js";
const DimensionsStateRenderer = (props) => {
  if (props.accessorTreeState.name === "initialized") {
    return /* @__PURE__ */ jsxDEV(Fragment, { children: props.children }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsSwitcher.tsx",
      lineNumber: 28,
      columnNumber: 10
    }, void 0);
  }
  return /* @__PURE__ */ jsxDEV(Spinner, {}, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsSwitcher.tsx",
    lineNumber: 30,
    columnNumber: 9
  }, void 0);
};
const DimensionsSwitcher = 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 BindingError(
      `DimensionSwitcher: 'minItems' for dimension ${props.dimension} must be no greater than 'maxItems'.`
    );
  }
  if (minItems < 1) {
    throw new BindingError(`DimensionSwitcher: 'minItems' for dimension ${props.dimension} must be at least 1.`);
  }
  const environment = useEnvironment();
  const qualifiedEntityList = QueryLanguage.desugarQualifiedEntityList(
    {
      orderBy: props.orderBy,
      entities: props.optionEntities
    },
    environment
  );
  const key = useMemo(() => JSON.stringify(qualifiedEntityList), [qualifiedEntityList]);
  const labelFactory = /* @__PURE__ */ jsxDEV(Field, { field: props.labelField }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsSwitcher.tsx",
    lineNumber: 58,
    columnNumber: 23
  }, void 0);
  return /* @__PURE__ */ jsxDEV(DataBindingProvider, { stateComponent: DimensionsStateRenderer, children: /* @__PURE__ */ jsxDEV(
    EntityListSubTree,
    {
      entities: qualifiedEntityList,
      orderBy: qualifiedEntityList.orderBy,
      offset: qualifiedEntityList.offset,
      limit: qualifiedEntityList.limit,
      listComponent: DimensionsRenderer,
      listProps: {
        buttonProps: props.buttonProps,
        dimension: props.dimension,
        labelFactory,
        minItems,
        maxItems,
        renderSelected: props.renderSelected,
        slugField: props.slugField
      },
      children: [
        labelFactory,
        /* @__PURE__ */ jsxDEV(Field, { field: props.slugField }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsSwitcher.tsx",
          lineNumber: 79,
          columnNumber: 5
        }, void 0)
      ]
    },
    void 0,
    true,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsSwitcher.tsx",
      lineNumber: 62,
      columnNumber: 4
    },
    void 0
  ) }, key, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsSwitcher.tsx",
    lineNumber: 61,
    columnNumber: 3
  }, void 0);
});
DimensionsSwitcher.displayName = "DimensionsSwitcher";
export {
  DimensionsSwitcher
};
//# sourceMappingURL=DimensionsSwitcher.js.map
