var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
import { PureComponent, createElement } from "react";
import { EnvironmentContext, BindingError, HasOne } from "@contember/binding";
import { LabelMiddlewareProvider } from "./LabelMiddleware.js";
const _SideDimensions = class extends PureComponent {
  render() {
    return /* @__PURE__ */ createElement("div", {
      className: "sideDimensions"
    }, /* @__PURE__ */ createElement(EnvironmentContext.Consumer, null, (oldEnvironment) => _SideDimensions.staticRender(this.props, oldEnvironment)));
  }
  static staticRender(props, environment) {
    if (props.dimension === void 0 === (props.staticOptions === void 0)) {
      throw new BindingError(`The SideDimensions component needs to be passed exactly one of its 'dimension' or 'staticOptions' props.`);
    }
    let dimensions;
    if (props.dimension !== void 0) {
      const selectedDimensions = environment.getAllDimensions();
      if (!(props.dimension in selectedDimensions)) {
        console.error(new BindingError(`The '${props.dimension}' dimension in undefined`));
        return null;
      }
      dimensions = selectedDimensions[props.dimension];
    } else if (props.staticOptions !== void 0) {
      dimensions = props.staticOptions;
    }
    const alignChildren = props.alignChildren !== false;
    const children = Array.isArray(props.children) && alignChildren ? props.children : [props.children];
    return /* @__PURE__ */ createElement("div", {
      className: "sideDimensions-dimensions"
    }, children.map((child, i) => /* @__PURE__ */ createElement("div", {
      className: "sideDimensions-dimensions-in",
      key: i
    }, dimensions.map((item, j) => {
      return /* @__PURE__ */ createElement(_SideDimensions.SingleDimension, {
        environment,
        dimensionValue: item,
        variableName: props.variableName,
        variables: props.variables,
        hasOneField: props.hasOneField,
        key: j,
        renderDimensionValue: dimensions.length > 1,
        labelMiddleware: props.labelMiddleware
      }, child);
    }))));
  }
};
let SideDimensions = _SideDimensions;
__publicField(SideDimensions, "displayName", "SideDimensions");
((SideDimensions2) => {
  const _SingleDimension = class extends PureComponent {
    render() {
      const children = _SingleDimension.staticRender(this.props, this.props.environment);
      const newLabelMiddleware = this.getLabelMiddleware();
      const inner = /* @__PURE__ */ createElement(EnvironmentContext.Provider, {
        value: _SingleDimension.generateEnvironment(this.props, this.props.environment)
      }, /* @__PURE__ */ createElement("div", {
        className: "sideDimensions-dimensions-dimension"
      }, this.props.renderDimensionValue && /* @__PURE__ */ createElement("span", {
        className: "sideDimensions-dimensions-dimensionValue"
      }, this.props.dimensionValue), children));
      if (newLabelMiddleware) {
        return /* @__PURE__ */ createElement(LabelMiddlewareProvider, {
          value: newLabelMiddleware
        }, inner);
      }
      return inner;
    }
    getLabelMiddleware() {
      if (this.props.labelMiddleware) {
        return this.props.labelMiddleware;
      }
      if (!this.props.variables) {
        return void 0;
      }
      const variables = typeof this.props.variables === "function" ? this.props.variables(this.props.dimensionValue) : this.props.variables;
      if ("labelMiddleware" in variables) {
        return variables.labelMiddleware;
      }
      return void 0;
    }
    static staticRender(props, environment) {
      if (!props.hasOneField) {
        return props.children;
      }
      const hasOneProps = typeof props.hasOneField === "string" ? {
        field: props.hasOneField
      } : props.hasOneField;
      return /* @__PURE__ */ createElement(HasOne, {
        ...hasOneProps
      }, props.children);
    }
    static generateEnvironment(props, oldEnvironment) {
      const deltaFactory = typeof props.variables === "function" ? props.variables(props.dimensionValue) : props.variables ?? {};
      if (props.variableName) {
        deltaFactory[props.variableName] = props.dimensionValue;
      }
      const { labelMiddleware, ...variables } = deltaFactory;
      return oldEnvironment.withVariables(variables);
    }
  };
  let SingleDimension = _SingleDimension;
  __publicField(SingleDimension, "displayName", "SideDimension");
  SideDimensions2.SingleDimension = SingleDimension;
})(SideDimensions || (SideDimensions = {}));
export { SideDimensions };
//# sourceMappingURL=SideDimensions.js.map
