"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const ui = require("@contember/ui");
const react = require("react");
const GenericVariableEdit = ({ label, value, onChange }) => {
  const [localValues, setLocalValues] = react.useState(value.length > 0 ? value : [""]);
  const onChangeCallback = react.useCallback((index, newValue) => {
    const newValues = newValue !== null ? localValues.map((v, i) => i === index ? newValue : v) : localValues.filter((_, i) => i !== index);
    setLocalValues(newValues);
    onChange(newValues.map((it) => it.trim()).filter((it) => !!it));
  }, [localValues, onChange, setLocalValues]);
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label, gap: "gap", style: { margin: "1em" }, children: [
    localValues.map((v, i) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Box, { padding: false, horizontal: true, gap: false, children: [
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        ui.TextInput,
        {
          notNull: true,
          value: v,
          distinction: "seamless",
          onChange: (newValue) => {
            onChangeCallback(i, newValue ?? "");
          },
          style: { marginLeft: "0.5em" }
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
          lineNumber: 30,
          columnNumber: 6
        },
        void 0
      ),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        ui.Button,
        {
          distinction: "seamless",
          onClick: () => {
            onChangeCallback(i, null);
          },
          children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Icon, { blueprintIcon: "trash" }, void 0, false, {
            fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
            lineNumber: 45,
            columnNumber: 7
          }, void 0)
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
          lineNumber: 39,
          columnNumber: 6
        },
        void 0
      )
    ] }, i, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
      lineNumber: 29,
      columnNumber: 5
    }, void 0)),
    /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      ui.Button,
      {
        distinction: "seamless",
        justify: "start",
        onClick: () => {
          setLocalValues((prev) => [...prev, ""]);
        },
        children: [
          /* @__PURE__ */ jsxDevRuntime.jsxDEV(
            ui.Icon,
            {
              blueprintIcon: "add",
              style: {
                marginRight: "0.2em",
                position: "relative",
                top: "0.05em"
              }
            },
            void 0,
            false,
            {
              fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
              lineNumber: 56,
              columnNumber: 5
            },
            void 0
          ),
          "Add"
        ]
      },
      void 0,
      true,
      {
        fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
        lineNumber: 49,
        columnNumber: 4
      },
      void 0
    )
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
    lineNumber: 27,
    columnNumber: 3
  }, void 0);
};
const VariableSelector = ({ rolesConfig, membership, variable, onChange }) => {
  const innerOnChange = react.useCallback(
    (newValues) => {
      const newMembership = {
        ...membership,
        variables: [
          ...membership.variables.filter((membershipVariable) => membershipVariable.name !== variable.name),
          {
            name: variable.name,
            values: newValues
          }
        ]
      };
      onChange(newMembership);
    },
    [membership, onChange, variable.name]
  );
  const roleConfig = rolesConfig?.[membership.role];
  const variableConfig = roleConfig?.variables[variable.name];
  if (roleConfig && variableConfig === void 0) {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: "Unknown variable" }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
      lineNumber: 94,
      columnNumber: 10
    }, void 0);
  }
  const variableInMembership = membership.variables.find(
    (membershipVariable) => membershipVariable.name === variable.name
  );
  const values = variableInMembership !== void 0 ? variableInMembership.values : [];
  if (variableConfig?.render) {
    const Component = variableConfig.render;
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(Component, { value: values, onChange: innerOnChange }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
      lineNumber: 104,
      columnNumber: 10
    }, void 0);
  } else {
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(GenericVariableEdit, { label: variable.name, value: values, onChange: innerOnChange }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/VariableSelector.tsx",
      lineNumber: 106,
      columnNumber: 10
    }, void 0);
  }
};
exports.VariableSelector = VariableSelector;
//# sourceMappingURL=VariableSelector.cjs.map
