import { CheckField } from "@ax/components";

import * as S from "./style";

const UniqueCheck = (props: IProps) => {
	const { name, value, options, onChange, disabled, inversed = false, className, description } = props;
	const uniqueOption = options[0];

	const handleChange = (changeEvt: ICheckEvent) => {
		const { isChecked } = changeEvt;
		onChange(isChecked);
	};

	const classes = description ? "with-subtitle" : "";

	return (
		<S.Wrapper className={classes}>
			<CheckField
				name={name}
				checked={!!value && value !== "false"}
				title={uniqueOption.title}
				value={value}
				onChange={handleChange}
				disabled={disabled}
				inversed={inversed}
				className={className}
			/>
			<S.Subtitle>{description}</S.Subtitle>
		</S.Wrapper>
	);
};

interface ICheckEvent {
	value: string;
	isChecked: boolean;
}

interface IProps {
	name: string;
	value: string | number;
	options: ICheck[];
	onChange(isChecked: boolean): void;
	disabled?: boolean;
	inversed?: boolean;
	className?: string;
	description?: string;
}

interface ICheck {
	title: string;
}

export default UniqueCheck;
