'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index = require('../../utils/index.js');
var base$1 = require('./variations/_view/base.js');
var base = require('./variations/_size/base.js');
var base$2 = require('./variations/_disabled/base.js');
var base$3 = require('./variations/_focused/base.js');
var IconsSvg = require('./IconsSvg.js');
var Checkbox_styles = require('./Checkbox.styles.js');
var Checkbox_tokens = require('./Checkbox.tokens.js');
var useForkRef = require('../../hooks/useForkRef.js');
var react = require('../../utils/react.js');
var extractTextFrom = require('../../utils/extractTextFrom.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

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__*/React.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 = _rollupPluginBabelHelpers.objectWithoutProperties(props, _excluded);
    var inputRef = React.useRef(null);
    var forkRef = useForkRef.useForkRef(inputRef, ref);
    React.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 = react.safeUseId();
    var uniqLabelId = "label-".concat(uniqId);
    var uniqDescriptionId = "description-".concat(uniqId);
    var checkboxId = id || "input-".concat(uniqId);
    var ariaLabel = React.useMemo(function () {
      return ariaLabelExternal || extractTextFrom.extractTextFrom(label);
    }, [ariaLabelExternal, label]);
    var isLabelAriaHidden = typeof label === 'string';
    var canFocused = focused ? 0 : -1;
    var hasContent = label || description;
    var singleLineClass = singleLine ? Checkbox_tokens.classes.singleLine : undefined;
    var onlyDescriptionClass = !label && description ? Checkbox_tokens.classes.onlyDescription : undefined;

    // Временное решение
    var getIcon = function getIcon() {
      if (checkIcon) {
        return checkIcon;
      }
      if (appearance === 'outline') {
        return _DoneThin || (_DoneThin = /*#__PURE__*/React__default.default.createElement(IconsSvg.DoneThin, null));
      }
      return _Done || (_Done = /*#__PURE__*/React__default.default.createElement(IconsSvg.Done, null));
    };
    var getIndeterminate = function getIndeterminate() {
      if (indeterminateIcon) {
        return indeterminateIcon;
      }
      if (appearance === 'outline') {
        return _IndeterminateThin || (_IndeterminateThin = /*#__PURE__*/React__default.default.createElement(IconsSvg.IndeterminateThin, null));
      }
      return _Indeterminate || (_Indeterminate = /*#__PURE__*/React__default.default.createElement(IconsSvg.Indeterminate, null));
    };
    return /*#__PURE__*/React__default.default.createElement(Root, {
      view: view,
      size: size,
      disabled: disabled,
      focused: focused,
      style: style,
      className: className,
      tabIndex: -1
    }, /*#__PURE__*/React__default.default.createElement(Checkbox_styles.StyledInput, _rollupPluginBabelHelpers.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.default.createElement(Checkbox_styles.StyledContentWrapper, {
      htmlFor: checkboxId,
      className: singleLineClass
    }, /*#__PURE__*/React__default.default.createElement(Checkbox_styles.StyledTriggerWrapper, null, /*#__PURE__*/React__default.default.createElement(Checkbox_styles.StyledTrigger, {
      className: Checkbox_tokens.classes.checkboxTrigger
    }, indeterminate ? getIndeterminate() : getIcon())), hasContent && /*#__PURE__*/React__default.default.createElement(Checkbox_styles.StyledContent, {
      className: singleLineClass
    }, label && /*#__PURE__*/React__default.default.createElement(Checkbox_styles.StyledLabel, {
      className: singleLineClass,
      id: uniqLabelId,
      "aria-hidden": isLabelAriaHidden
    }, label), description && /*#__PURE__*/React__default.default.createElement(Checkbox_styles.StyledDescription, {
      className: index.cx(singleLineClass, onlyDescriptionClass),
      id: uniqDescriptionId
    }, description))));
  });
};
var checkboxConfig = {
  name: 'Checkbox',
  tag: 'div',
  layout: checkboxRoot,
  base: Checkbox_styles.base,
  variations: {
    size: {
      css: base.base
    },
    view: {
      css: base$1.base
    },
    disabled: {
      css: base$2.base
    },
    focused: {
      css: base$3.base
    }
  },
  defaults: {
    size: 'm',
    view: 'accent'
  }
};

exports.checkboxConfig = checkboxConfig;
exports.checkboxRoot = checkboxRoot;
