/*
* Copyright 2025 Commonwealth Scientific and Industrial Research
* Organisation (CSIRO) ABN 41 687 119 230.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { useMemo } from 'react';
import useAnswerOptionsToggleExpressions from '../../../hooks/useAnswerOptionsToggleExpressions';
import useReadOnly from '../../../hooks/useReadOnly';
import useValidationFeedbackSeverity from '../../../hooks/useValidationFeedbackSeverity';
import useValueSetCodings from '../../../hooks/useValueSetCodings';
import type { BaseItemProps } from '../../../interfaces/renderProps.interface';
import { useQuestionnaireStore } from '../../../stores';
import { convertCodingsToAnswerOptions, findInAnswerOptions } from '../../../utils/choice';
import { createEmptyQrItem, getQRItemId } from '../../../utils/qrItem';
import { FullWidthFormComponentBox } from '../../Box.styles';
import ItemFieldGrid from '../ItemParts/ItemFieldGrid';
import ItemLabel from '../ItemParts/ItemLabel';
import ChoiceRadioAnswerValueSetFields from './ChoiceRadioAnswerValueSetFields';
function ChoiceRadioAnswerValueSetItem(props: BaseItemProps) {
const {
qItem,
qrItem,
isRepeated,
isTabled,
renderingExtensions,
parentIsReadOnly,
feedbackFromParent,
calcExpUpdated,
onQrItemChange
} = props;
const onFocusLinkId = useQuestionnaireStore.use.onFocusLinkId();
// Init input value
const answerKey = getQRItemId(qrItem?.answer?.[0]?.id);
const qrChoiceRadio = qrItem ?? createEmptyQrItem(qItem, answerKey);
let valueRadio: string | null = null;
if (qrChoiceRadio.answer) {
valueRadio =
qrChoiceRadio.answer[0].valueCoding?.code ??
qrChoiceRadio.answer[0].valueCoding?.display ??
null;
}
// Get codings/options from valueSet
const { codings, terminologyError, dynamicCodingsUpdated } = useValueSetCodings(qItem);
const readOnly = useReadOnly(qItem, parentIsReadOnly);
// Perform validation checks - there's no string-based input here
const { feedback, feedbackSeverity } = useValidationFeedbackSeverity(qItem, feedbackFromParent);
const { displayInstructions } = renderingExtensions;
const instructionsId =
displayInstructions && !feedback ? `instructions-${qItem.linkId}` : undefined;
const options = useMemo(() => convertCodingsToAnswerOptions(codings), [codings]);
// Process answerOptionsToggleExpressions
const { answerOptionsToggleExpressionsMap, answerOptionsToggleExpUpdated } =
useAnswerOptionsToggleExpressions(qItem.linkId);
function handleChange(newValue: string) {
if (codings.length > 0) {
const qrAnswer = findInAnswerOptions(options, newValue);
const emptyQrItem = createEmptyQrItem(qItem, answerKey);
onQrItemChange(
qrAnswer ? { ...emptyQrItem, answer: [{ ...qrAnswer, id: answerKey }] } : emptyQrItem
);
}
}
function handleClear() {
onQrItemChange(createEmptyQrItem(qItem, answerKey));
}
if (isRepeated) {
return (
);
}
return (
onFocusLinkId(qItem.linkId)}>
}
fieldChildren={
}
/>
);
}
export default ChoiceRadioAnswerValueSetItem;