function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
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 var checkboxRoot = function checkboxRoot(Root) {
    return /*#__PURE__*/ forwardRef(function(props, ref) {
        var size = props.size, view = props.view, focused = props.focused, disabled = props.disabled, id = props.id, label = props.label, description = props.description, indeterminate = props.indeterminate, appearance = props.appearance, style = props.style, className = props.className, _props_singleLine = props.singleLine, singleLine = _props_singleLine === void 0 ? false : _props_singleLine, ariaLabelExternal = props['aria-label'], onChange = props.onChange, onClick = props.onClick, rest = _object_without_properties(props, [
            "size",
            "view",
            "focused",
            "disabled",
            "id",
            "label",
            "description",
            "indeterminate",
            "appearance",
            "style",
            "className",
            "singleLine",
            'aria-label',
            "onChange",
            "onClick"
        ]);
        var inputRef = useRef(null);
        var forkRef = useForkRef(inputRef, ref);
        useEffect(function() {
            if (inputRef.current) {
                inputRef.current.indeterminate = Boolean(indeterminate);
            }
        }, [
            inputRef,
            indeterminate
        ]);
        var handleClick = function handleClick(e) {
            if (indeterminate && !onChange) {
                e.preventDefault();
            }
            if (onClick) {
                onClick(e);
            }
        };
        var uniqId = safeUseId();
        var uniqLabelId = "label-".concat(uniqId);
        var uniqDescriptionId = "description-".concat(uniqId);
        var checkboxId = id || "input-".concat(uniqId);
        var ariaLabel = useMemo(function() {
            return ariaLabelExternal || extractTextFrom(label);
        }, [
            ariaLabelExternal,
            label
        ]);
        var isLabelAriaHidden = typeof label === 'string';
        var canFocused = focused ? 0 : -1;
        var hasContent = label || description;
        var singleLineClass = singleLine ? classes.singleLine : undefined;
        var onlyDescriptionClass = !label && description ? classes.onlyDescription : undefined;
        // Временное решение
        var getIcon = function getIcon() {
            if (appearance === 'outline') {
                return /*#__PURE__*/ React.createElement(DoneThin, null);
            }
            return /*#__PURE__*/ React.createElement(Done, null);
        };
        var getIndeterminate = function getIndeterminate() {
            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, _object_spread_props(_object_spread({}, 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 var checkboxConfig = {
    name: 'Checkbox',
    tag: 'div',
    layout: checkboxRoot,
    base: base,
    variations: {
        size: {
            css: sizeCSS
        },
        view: {
            css: viewCSS
        },
        disabled: {
            css: disabledCSS
        },
        focused: {
            css: focusedCSS
        }
    },
    defaults: {
        size: 'm',
        view: 'accent'
    }
};
