import React, { forwardRef, useEffect, useMemo, useRef } from "react";
import { safeUseId, cx, extractTextFrom } from "../../utils";
import { useForkRef } from "../../hooks";
import { base as viewCSS } from "./variations/_view/base";
import { base as sizeCSS } from "./variations/_size/base";
import { base as disabledCSS } from "./variations/_disabled/base";
import { base as focusedCSS } from "./variations/_focused/base";
import { Done, Indeterminate, DoneThin, IndeterminateThin } from "./IconsSvg";
import { StyledContent, StyledContentWrapper, StyledDescription, StyledInput, StyledLabel, StyledTrigger, base, StyledTriggerWrapper } from "./Checkbox.styles";
import { classes } from "./Checkbox.tokens";
export const checkboxRoot = (Root)=>/*#__PURE__*/ forwardRef((props, ref)=>{
        const { size, view, focused, disabled, id, label, description, indeterminate, appearance, checkIcon, indeterminateIcon, style, className, singleLine = false, 'aria-label': ariaLabelExternal, onChange, onClick, ...rest } = props;
        const inputRef = useRef(null);
        const forkRef = useForkRef(inputRef, ref);
        useEffect(()=>{
            if (inputRef.current) {
                inputRef.current.indeterminate = Boolean(indeterminate);
            }
        }, [
            inputRef,
            indeterminate
        ]);
        const handleClick = (e)=>{
            if (indeterminate && !onChange) {
                e.preventDefault();
            }
            if (onClick) {
                onClick(e);
            }
        };
        const uniqId = safeUseId();
        const uniqLabelId = `label-${uniqId}`;
        const uniqDescriptionId = `description-${uniqId}`;
        const checkboxId = id || `input-${uniqId}`;
        const ariaLabel = useMemo(()=>ariaLabelExternal || extractTextFrom(label), [
            ariaLabelExternal,
            label
        ]);
        const isLabelAriaHidden = typeof label === 'string';
        const canFocused = focused ? 0 : -1;
        const hasContent = label || description;
        const singleLineClass = singleLine ? classes.singleLine : undefined;
        const onlyDescriptionClass = !label && description ? classes.onlyDescription : undefined;
        // Временное решение
        const getIcon = ()=>{
            if (checkIcon) {
                return checkIcon;
            }
            if (appearance === 'outline') {
                return /*#__PURE__*/ React.createElement(DoneThin, null);
            }
            return /*#__PURE__*/ React.createElement(Done, null);
        };
        const getIndeterminate = ()=>{
            if (indeterminateIcon) {
                return indeterminateIcon;
            }
            if (appearance === 'outline') {
                return /*#__PURE__*/ React.createElement(IndeterminateThin, null);
            }
            return /*#__PURE__*/ React.createElement(Indeterminate, null);
        };
        return /*#__PURE__*/ React.createElement(Root, {
            view: view,
            size: size,
            disabled: disabled,
            focused: focused,
            style: style,
            className: className,
            tabIndex: -1
        }, /*#__PURE__*/ React.createElement(StyledInput, {
            ...rest,
            id: checkboxId,
            ref: forkRef,
            type: "checkbox",
            disabled: disabled,
            "aria-label": ariaLabel,
            "aria-describedby": uniqDescriptionId,
            tabIndex: canFocused,
            onChange: onChange,
            onClick: handleClick
        }), /*#__PURE__*/ React.createElement(StyledContentWrapper, {
            htmlFor: checkboxId,
            className: singleLineClass
        }, /*#__PURE__*/ React.createElement(StyledTriggerWrapper, null, /*#__PURE__*/ React.createElement(StyledTrigger, {
            className: classes.checkboxTrigger
        }, indeterminate ? getIndeterminate() : getIcon())), hasContent && /*#__PURE__*/ React.createElement(StyledContent, {
            className: singleLineClass
        }, label && /*#__PURE__*/ React.createElement(StyledLabel, {
            className: singleLineClass,
            id: uniqLabelId,
            "aria-hidden": isLabelAriaHidden
        }, label), description && /*#__PURE__*/ React.createElement(StyledDescription, {
            className: cx(singleLineClass, onlyDescriptionClass),
            id: uniqDescriptionId
        }, description))));
    });
export const checkboxConfig = {
    name: 'Checkbox',
    tag: 'div',
    layout: checkboxRoot,
    base,
    variations: {
        size: {
            css: sizeCSS
        },
        view: {
            css: viewCSS
        },
        disabled: {
            css: disabledCSS
        },
        focused: {
            css: focusedCSS
        }
    },
    defaults: {
        size: 'm',
        view: 'accent'
    }
};
