import { InputControl } from '../ui/input-control';
import { Flex, FlexItem, FlexBlock } from '../ui/flex';
import { Text } from '../ui/text';
import { PlusCircle, XCircle } from 'lucide-react';
import { Controller, useFormContext, useWatch } from 'react-hook-form';
interface FhirPeriodProps {
label: string;
context: string;
type?: 'date' | 'dateTime';
disabled?: boolean;
readonly?: boolean;
alwaysExpanded?: boolean;
}
const FhirPeriod = ({ label, context, type = 'date', disabled = false, readonly = false, alwaysExpanded = false }: FhirPeriodProps) => {
const { control, setValue, getValues } = useFormContext();
const periodValue = useWatch({
control,
name: context,
defaultValue: undefined,
});
const startValue = useWatch({
control,
name: `${context}.start`,
defaultValue: '',
});
const hasPeriod = !!periodValue;
const hasStart = periodValue?.start !== undefined;
const hasEnd = periodValue?.end !== undefined;
const getCurrentValue = () => {
const now = new Date();
if (type === 'dateTime') {
return now.toISOString();
}
return now.toISOString().split('T')[0];
};
const formatValueForInput = (value: string) => {
if (!value) return '';
if (type === 'dateTime') {
const d = new Date(value);
const year = d.getFullYear();
const month = String(d.getMonth() + 1).padStart(2, '0');
const day = String(d.getDate()).padStart(2, '0');
const hours = String(d.getHours()).padStart(2, '0');
const minutes = String(d.getMinutes()).padStart(2, '0');
return `${year}-${month}-${day}T${hours}:${minutes}`;
}
return new Date(value).toISOString().split('T')[0];
};
const formatValueForStorage = (value: string | undefined) => {
if (!value) return '';
if (type === 'dateTime') {
return new Date(value).toISOString();
}
return new Date(value).toISOString().split('T')[0];
};
const startMin = startValue ? formatValueForInput(startValue) : undefined;
const addPeriod = () => {
const currentValue = getCurrentValue();
setValue(context, { start: currentValue }, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: true
});
};
const addEndPeriod = () => {
if (hasPeriod && !hasEnd) {
const currentValue = getCurrentValue();
const existingValue = getValues(context) || {};
setValue(context, {
...existingValue,
end: currentValue
}, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: true
});
}
};
const removePeriod = () => {
setValue(context, undefined, {
shouldDirty: true,
shouldTouch: true,
shouldValidate: true
});
};
const inputType = type === 'dateTime' ? 'datetime-local' : 'date';
const startLabel = type === 'dateTime' ? 'Start Date & Time' : 'Start';
const endLabel = type === 'dateTime' ? 'End Date & Time' : 'End';
if (alwaysExpanded) {
return (