"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get checkboxConfig () {
        return checkboxConfig;
    },
    get checkboxRoot () {
        return checkboxRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../utils");
const _hooks = require("../../hooks");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _base2 = require("./variations/_disabled/base");
const _base3 = require("./variations/_focused/base");
const _IconsSvg = require("./IconsSvg");
const _Checkboxstyles = require("./Checkbox.styles");
const _Checkboxtokens = require("./Checkbox.tokens");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const checkboxRoot = (Root)=>/*#__PURE__*/ (0, _react.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 = (0, _react.useRef)(null);
        const forkRef = (0, _hooks.useForkRef)(inputRef, ref);
        (0, _react.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 = (0, _utils.safeUseId)();
        const uniqLabelId = `label-${uniqId}`;
        const uniqDescriptionId = `description-${uniqId}`;
        const checkboxId = id || `input-${uniqId}`;
        const ariaLabel = (0, _react.useMemo)(()=>ariaLabelExternal || (0, _utils.extractTextFrom)(label), [
            ariaLabelExternal,
            label
        ]);
        const isLabelAriaHidden = typeof label === 'string';
        const canFocused = focused ? 0 : -1;
        const hasContent = label || description;
        const singleLineClass = singleLine ? _Checkboxtokens.classes.singleLine : undefined;
        const onlyDescriptionClass = !label && description ? _Checkboxtokens.classes.onlyDescription : undefined;
        // Временное решение
        const getIcon = ()=>{
            if (checkIcon) {
                return checkIcon;
            }
            if (appearance === 'outline') {
                return /*#__PURE__*/ _react.default.createElement(_IconsSvg.DoneThin, null);
            }
            return /*#__PURE__*/ _react.default.createElement(_IconsSvg.Done, null);
        };
        const getIndeterminate = ()=>{
            if (indeterminateIcon) {
                return indeterminateIcon;
            }
            if (appearance === 'outline') {
                return /*#__PURE__*/ _react.default.createElement(_IconsSvg.IndeterminateThin, null);
            }
            return /*#__PURE__*/ _react.default.createElement(_IconsSvg.Indeterminate, null);
        };
        return /*#__PURE__*/ _react.default.createElement(Root, {
            view: view,
            size: size,
            disabled: disabled,
            focused: focused,
            style: style,
            className: className,
            tabIndex: -1
        }, /*#__PURE__*/ _react.default.createElement(_Checkboxstyles.StyledInput, {
            ...rest,
            id: checkboxId,
            ref: forkRef,
            type: "checkbox",
            disabled: disabled,
            "aria-label": ariaLabel,
            "aria-describedby": uniqDescriptionId,
            tabIndex: canFocused,
            onChange: onChange,
            onClick: handleClick
        }), /*#__PURE__*/ _react.default.createElement(_Checkboxstyles.StyledContentWrapper, {
            htmlFor: checkboxId,
            className: singleLineClass
        }, /*#__PURE__*/ _react.default.createElement(_Checkboxstyles.StyledTriggerWrapper, null, /*#__PURE__*/ _react.default.createElement(_Checkboxstyles.StyledTrigger, {
            className: _Checkboxtokens.classes.checkboxTrigger
        }, indeterminate ? getIndeterminate() : getIcon())), hasContent && /*#__PURE__*/ _react.default.createElement(_Checkboxstyles.StyledContent, {
            className: singleLineClass
        }, label && /*#__PURE__*/ _react.default.createElement(_Checkboxstyles.StyledLabel, {
            className: singleLineClass,
            id: uniqLabelId,
            "aria-hidden": isLabelAriaHidden
        }, label), description && /*#__PURE__*/ _react.default.createElement(_Checkboxstyles.StyledDescription, {
            className: (0, _utils.cx)(singleLineClass, onlyDescriptionClass),
            id: uniqDescriptionId
        }, description))));
    });
const checkboxConfig = {
    name: 'Checkbox',
    tag: 'div',
    layout: checkboxRoot,
    base: _Checkboxstyles.base,
    variations: {
        size: {
            css: _base1.base
        },
        view: {
            css: _base.base
        },
        disabled: {
            css: _base2.base
        },
        focused: {
            css: _base3.base
        }
    },
    defaults: {
        size: 'm',
        view: 'accent'
    }
};
