"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const reactUtils = require("@contember/react-utils");
const ui = require("@contember/ui");
const react = require("react");
const renderByJoining = require("./renderByJoining.cjs");
const RoutingLink = require("../../../../routing/RoutingLink.cjs");
const reactRouting = require("@contember/react-routing");
function DimensionsRenderer(props) {
  const environment = reactBinding.useEnvironment();
  const redirect = reactRouting.useRedirect();
  const renderSelected = react.useCallback((selectedDimensions2) => {
    const renderer = props.renderSelected || renderByJoining.renderByJoining;
    return renderer(selectedDimensions2);
  }, [props.renderSelected]);
  const renderContent = (dimensionData, selectedDimensions2) => {
    const canSelectJustOne = props.minItems === 1 && props.maxItems === 1;
    const selectedDimensionsCount = selectedDimensions2.length;
    const canSelectAnother = selectedDimensionsCount < props.maxItems;
    const canSelectLess = selectedDimensionsCount > props.minItems;
    const getRequestChangeCallback = (dimension) => (reqState) => {
      if (reqState === null) {
        throw "Cannot switch dimension of unmatched request";
      }
      let updatedDimensions;
      if (!dimension.isSelected && !canSelectAnother) {
        updatedDimensions = [...selectedDimensions2.slice(1), dimension];
      } else if (dimension.isSelected && !canSelectLess) {
        updatedDimensions = selectedDimensions2;
      } else {
        updatedDimensions = dimensionData.filter((item) => {
          if (item.slug === dimension.slug) {
            return !item.isSelected;
          }
          return item.isSelected;
        });
      }
      return {
        ...reqState,
        dimensions: {
          ...reqState.dimensions || {},
          [props.dimension]: getUniqueDimensions(updatedDimensions.map((item) => item.slug).slice(0, props.maxItems))
        }
      };
    };
    const renderedDimensions = dimensionData.map((dimension) => {
      if (canSelectJustOne) {
        return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
          RoutingLink.RoutingLink,
          {
            to: getRequestChangeCallback(dimension),
            Component: ({ href, onClick }) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(
              ui.AnchorButton,
              {
                href,
                display: "block",
                active: dimension.isSelected,
                onClick,
                children: dimension.label
              },
              void 0,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
                lineNumber: 79,
                columnNumber: 8
              },
              this
            )
          },
          dimension.slug,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
            lineNumber: 75,
            columnNumber: 6
          },
          this
        );
      } else {
        return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
          ui.FieldContainer,
          {
            display: "inline",
            label: dimension.label,
            labelPosition: "right",
            children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
              ui.Checkbox,
              {
                value: dimension.isSelected,
                disabled: dimension.isSelected && !canSelectLess,
                onChange: () => redirect(getRequestChangeCallback(dimension))
              },
              dimension.slug,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
                lineNumber: 98,
                columnNumber: 7
              },
              this
            )
          },
          dimension.slug,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
            lineNumber: 92,
            columnNumber: 6
          },
          this
        );
      }
    });
    if (canSelectJustOne) {
      return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.ButtonGroup, { direction: "vertical", display: "block", inset: "border", children: renderedDimensions }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
        lineNumber: 110,
        columnNumber: 11
      }, this);
    }
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Box, { border: false, gap: "gutter", padding: "gutter", children: renderedDimensions }, "multipleDimensions", false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
      lineNumber: 114,
      columnNumber: 4
    }, this);
  };
  const getNormalizedData = (currentDimensions, accessor) => {
    const entities = Array.from(accessor);
    const normalized = [];
    for (const entity of entities) {
      if (!(entity instanceof reactBinding.EntityAccessor)) {
        continue;
      }
      const slugField = entity.getField(props.slugField);
      if (typeof slugField.value === "string") {
        const slugValue = slugField.value;
        normalized.push({
          slug: slugValue,
          isSelected: currentDimensions.indexOf(slugValue) !== -1,
          label: /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.Entity, { accessor: entity, children: props.labelFactory }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
            lineNumber: 134,
            columnNumber: 13
          }, this)
        });
      }
    }
    return normalized;
  };
  const getUniqueDimensions = (selectedDimensions2) => {
    return selectedDimensions2.filter((item, i, array) => array.indexOf(item) === i);
  };
  const uniqueDimensions = getUniqueDimensions(environment.getDimensionOrElse(props.dimension, []));
  const normalizedData = getNormalizedData(uniqueDimensions, props.accessor);
  const selectedDimensions = normalizedData ? uniqueDimensions.filter((slug) => normalizedData.find((item) => item.slug === slug) !== void 0).map((dimension) => normalizedData.find((item) => item.slug === dimension)).filter((item) => !!item && item.isSelected) : reactUtils.emptyArray;
  react.useEffect(() => {
    const redirectTarget = selectedDimensions.length === 0 ? normalizedData || [] : selectedDimensions;
    if (normalizedData !== void 0 && selectedDimensions.length === 0 && redirectTarget.length > 0) {
      redirect((requestState) => {
        return {
          ...requestState,
          dimensions: {
            ...requestState?.dimensions ?? {},
            [props.dimension]: getUniqueDimensions(redirectTarget.map((item) => item.slug).slice(0, props.maxItems))
          }
        };
      });
    }
  }, [normalizedData, props.dimension, props.maxItems, redirect, selectedDimensions]);
  const buttonProps = react.useMemo(() => ({
    distinction: "seamless",
    size: "small",
    ...props.buttonProps,
    children: renderSelected(selectedDimensions)
  }), [props.buttonProps, renderSelected, selectedDimensions]);
  if (normalizedData.length === 0) {
    return null;
  }
  if (normalizedData.length === 1) {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { className: "dimensionsSwitcher", children: /* @__PURE__ */ jsxDevRuntime.jsxDEV("div", { className: "dimensionsSwitcher-staticSelected", children: renderSelected(selectedDimensions) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
      lineNumber: 187,
      columnNumber: 5
    }, this) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
      lineNumber: 186,
      columnNumber: 4
    }, this);
  }
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Dropdown, { buttonProps, children: renderContent(normalizedData, selectedDimensions) }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/environment/DimensionsSwitcher/DimensionsRenderer.tsx",
    lineNumber: 193,
    columnNumber: 3
  }, this);
}
exports.DimensionsRenderer = DimensionsRenderer;
//# sourceMappingURL=DimensionsRenderer.cjs.map
