import React, { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { Controller, useFieldArray, useFormContext } from "react-hook-form";
import {
Button,
Select,
SelectOption,
SelectVariant,
TextInput,
} from "@patternfly/react-core";
import {
TableComposable,
Tbody,
Td,
Th,
Thead,
Tr,
} from "@patternfly/react-table";
import { MinusCircleIcon, PlusCircleIcon } from "@patternfly/react-icons";
import type ResourceRepresentation from "@keycloak/keycloak-admin-client/lib/defs/resourceRepresentation";
import { defaultContextAttributes } from "../utils";
import { camelCase } from "lodash-es";
import "../../components/attribute-form/attribute-form.css";
export type AttributeType = {
key?: string;
name: string;
custom?: boolean;
values?: {
[key: string]: string;
}[];
};
type AttributeInputProps = {
name: string;
selectableValues?: AttributeType[];
resources?: ResourceRepresentation[];
};
type ValueInputProps = {
name: string;
rowIndex: number;
attribute: any;
selectableValues?: AttributeType[];
resources?: ResourceRepresentation[];
};
const ValueInput = ({
name,
rowIndex,
attribute,
selectableValues,
resources,
}: ValueInputProps) => {
const { t } = useTranslation("common");
const { control, register, getValues } = useFormContext();
const [isValueOpenArray, setIsValueOpenArray] = useState([false]);
const toggleValueSelect = (rowIndex: number, open: boolean) => {
const arr = [...isValueOpenArray];
arr[rowIndex] = open;
setIsValueOpenArray(arr);
};
let attributeValues: { key: string; name: string }[] | undefined = [];
const scopeValues = resources?.find(
(resource) => resource.name === getValues().resources[rowIndex]?.key
)?.scopes;
if (selectableValues) {
attributeValues = defaultContextAttributes.find(
(attr) => attr.key === getValues().context[rowIndex]?.key
)?.values;
}
const renderSelectOptionType = () => {
if (attributeValues?.length && !resources) {
return attributeValues.map((attr) => (