"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 QueryLoader = require("../QueryLoader.cjs");
const VariableSelector = require("./VariableSelector.cjs");
const roles = require("../../queries/roles.cjs");
const EditMembership = ({ project, memberships, setMemberships, rolesConfig }) => {
  const { state: roleDefinitionState } = roles.useListRolesQuery(project);
  const addMembership = react.useCallback(() => {
    setMemberships((memberships2) => [...memberships2, void 0]);
  }, [setMemberships]);
  return /* @__PURE__ */ jsxDevRuntime.jsxDEV(QueryLoader.QueryLoader, { query: roleDefinitionState, children: ({ query }) => {
    const roleDefinitions = query.data.project.roles;
    const rolesToShow = rolesConfig ? roleDefinitions.filter(({ name }) => name in rolesConfig) : roleDefinitions;
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.FieldContainer, { label: "Roles", useLabelElement: false, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { gap: "gap", children: [
      memberships.map((membership, membershipIndex) => {
        const roleDefinition = membership && roleDefinitions.find((def) => def.name === membership.role);
        const variablesToShow = (roleDefinition && roleDefinition.variables.filter((it) => "entityName" in it)) ?? [];
        const updateMembership = (newMembership) => {
          setMemberships((memberships2) => {
            const newMemberships = [...memberships2];
            newMemberships[membershipIndex] = newMembership;
            return newMemberships;
          });
        };
        const removeMembership = () => {
          setMemberships((memberships2) => {
            return memberships2.slice(0, membershipIndex).concat(memberships2.slice(membershipIndex + 1));
          });
        };
        return /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Box, { padding: false, children: [
          /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { align: "center", horizontal: true, gap: false, children: [
            /* @__PURE__ */ jsxDevRuntime.jsxDEV(
              ui.Select,
              {
                required: true,
                distinction: "seamless-with-padding",
                onChange: (role) => {
                  if (typeof role === "string" && (!membership || role !== membership.role)) {
                    updateMembership({ role, variables: [] });
                  }
                },
                options: [
                  { value: null, label: "Select role", disabled: true },
                  ...rolesToShow.map(({ name: roleName }) => {
                    const otherIndex = memberships.findIndex((mem) => mem && mem.role === roleName);
                    const enabled = otherIndex === -1 || otherIndex === membershipIndex;
                    return {
                      value: roleName,
                      label: rolesConfig?.[roleName]?.name ?? roleName,
                      disabled: !enabled
                    };
                  })
                ],
                placeholder: "Select role",
                value: membership === void 0 ? null : membership.role
              },
              void 0,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
                lineNumber: 72,
                columnNumber: 12
              },
              void 0
            ),
            /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Divider, { gap: false }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
              lineNumber: 95,
              columnNumber: 12
            }, void 0),
            /* @__PURE__ */ jsxDevRuntime.jsxDEV(
              ui.Button,
              {
                square: true,
                distinction: "seamless",
                onClick: removeMembership,
                children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Icon, { blueprintIcon: "trash" }, void 0, false, {
                  fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
                  lineNumber: 101,
                  columnNumber: 13
                }, void 0)
              },
              void 0,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
                lineNumber: 96,
                columnNumber: 12
              },
              void 0
            )
          ] }, void 0, true, {
            fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
            lineNumber: 71,
            columnNumber: 11
          }, void 0),
          variablesToShow.length > 0 && membership && /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
            /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Divider, { gap: false }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
              lineNumber: 106,
              columnNumber: 12
            }, void 0),
            /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Box, { background: false, border: false, padding: false, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Stack, { children: variablesToShow.map((variable) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(
              VariableSelector.VariableSelector,
              {
                rolesConfig,
                membership,
                variable,
                onChange: (newMembership) => {
                  updateMembership(newMembership);
                }
              },
              variable.name,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
                lineNumber: 110,
                columnNumber: 15
              },
              void 0
            )) }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
              lineNumber: 108,
              columnNumber: 13
            }, void 0) }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
              lineNumber: 107,
              columnNumber: 12
            }, void 0)
          ] }, void 0, true, {
            fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
            lineNumber: 105,
            columnNumber: 56
          }, void 0)
        ] }, membershipIndex, true, {
          fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
          lineNumber: 70,
          columnNumber: 10
        }, void 0);
      }),
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        ui.Button,
        {
          distinction: "seamless",
          display: "block",
          justify: "start",
          onClick: addMembership,
          children: [
            /* @__PURE__ */ jsxDevRuntime.jsxDEV(ui.Icon, { blueprintIcon: "add", style: { marginRight: "0.2em" } }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
              lineNumber: 134,
              columnNumber: 9
            }, void 0),
            "Add role"
          ]
        },
        void 0,
        true,
        {
          fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
          lineNumber: 128,
          columnNumber: 8
        },
        void 0
      )
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
      lineNumber: 50,
      columnNumber: 7
    }, void 0) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
      lineNumber: 49,
      columnNumber: 6
    }, void 0);
  } }, void 0, false, {
    fileName: "/__w/interface/interface/packages/admin/src/tenant/components/member/EditMembership.tsx",
    lineNumber: 43,
    columnNumber: 3
  }, void 0);
};
exports.EditMembership = EditMembership;
//# sourceMappingURL=EditMembership.cjs.map
