import { objectWithoutProperties as _objectWithoutProperties, extends as _extends } from '../../_virtual/_rollupPluginBabelHelpers.js';
import React__default, { forwardRef, useRef, useEffect, useMemo } from 'react';
import { cx } from '../../utils/index.js';
import { base as base$2 } from './variations/_view/base.js';
import { base as base$1 } from './variations/_size/base.js';
import { base as base$3 } from './variations/_disabled/base.js';
import { base as base$4 } from './variations/_focused/base.js';
import { DoneThin, Done, IndeterminateThin, Indeterminate } from './IconsSvg.js';
import { StyledInput, StyledContentWrapper, StyledTriggerWrapper, StyledTrigger, StyledContent, StyledLabel, StyledDescription, base } from './Checkbox.styles.js';
import { classes } from './Checkbox.tokens.js';
import { useForkRef } from '../../hooks/useForkRef.js';
import { safeUseId } from '../../utils/react.js';
import { extractTextFrom } from '../../utils/extractTextFrom.js';

var _DoneThin, _Done, _IndeterminateThin, _Indeterminate;
var _excluded = ["size", "view", "focused", "disabled", "id", "label", "description", "indeterminate", "appearance", "checkIcon", "indeterminateIcon", "style", "className", "singleLine", "aria-label", "onChange", "onClick"];
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,
      checkIcon = props.checkIcon,
      indeterminateIcon = props.indeterminateIcon,
      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 = _objectWithoutProperties(props, _excluded);
    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 (checkIcon) {
        return checkIcon;
      }
      if (appearance === 'outline') {
        return _DoneThin || (_DoneThin = /*#__PURE__*/React__default.createElement(DoneThin, null));
      }
      return _Done || (_Done = /*#__PURE__*/React__default.createElement(Done, null));
    };
    var getIndeterminate = function getIndeterminate() {
      if (indeterminateIcon) {
        return indeterminateIcon;
      }
      if (appearance === 'outline') {
        return _IndeterminateThin || (_IndeterminateThin = /*#__PURE__*/React__default.createElement(IndeterminateThin, null));
      }
      return _Indeterminate || (_Indeterminate = /*#__PURE__*/React__default.createElement(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(StyledInput, _extends({}, 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(StyledContentWrapper, {
      htmlFor: checkboxId,
      className: singleLineClass
    }, /*#__PURE__*/React__default.createElement(StyledTriggerWrapper, null, /*#__PURE__*/React__default.createElement(StyledTrigger, {
      className: classes.checkboxTrigger
    }, indeterminate ? getIndeterminate() : getIcon())), hasContent && /*#__PURE__*/React__default.createElement(StyledContent, {
      className: singleLineClass
    }, label && /*#__PURE__*/React__default.createElement(StyledLabel, {
      className: singleLineClass,
      id: uniqLabelId,
      "aria-hidden": isLabelAriaHidden
    }, label), description && /*#__PURE__*/React__default.createElement(StyledDescription, {
      className: cx(singleLineClass, onlyDescriptionClass),
      id: uniqDescriptionId
    }, description))));
  });
};
var checkboxConfig = {
  name: 'Checkbox',
  tag: 'div',
  layout: checkboxRoot,
  base: base,
  variations: {
    size: {
      css: base$1
    },
    view: {
      css: base$2
    },
    disabled: {
      css: base$3
    },
    focused: {
      css: base$4
    }
  },
  defaults: {
    size: 'm',
    view: 'accent'
  }
};

export { checkboxConfig, checkboxRoot };
