var _excluded = ["className", "id", "checked", "defaultChecked", "description", "isDisabled", "isInvalid", "label", "name", "onChange", "onBlur", "value", "variant", "type", "partials"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) { ; } } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useRef, useState, useEffect } from 'react';
import mergeRefs from 'react-merge-refs';
import { CHECKBOX_RADIO_VARIANT } from './types';
import { StyledWrapper } from './partials/StyledWrapper';
import { StyledInnerWrapper } from './partials/StyledInnerWrapper';
import { StyledDescription } from './partials/StyledDescription';
import { StyledLabel } from './partials/StyledLabel';
import { StyledInput } from './partials/StyledInput';
import { useExternalId } from '../../hooks/useExternalId';
export var CheckboxRadioBase = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var _ref$className = _ref.className,
    className = _ref$className === void 0 ? '' : _ref$className,
    externalId = _ref.id,
    _ref$checked = _ref.checked,
    checked = _ref$checked === void 0 ? false : _ref$checked,
    defaultChecked = _ref.defaultChecked,
    description = _ref.description,
    _ref$isDisabled = _ref.isDisabled,
    isDisabled = _ref$isDisabled === void 0 ? false : _ref$isDisabled,
    isInvalid = _ref.isInvalid,
    label = _ref.label,
    name = _ref.name,
    onChange = _ref.onChange,
    onBlur = _ref.onBlur,
    value = _ref.value,
    _ref$variant = _ref.variant,
    variant = _ref$variant === void 0 ? CHECKBOX_RADIO_VARIANT.DEFAULT : _ref$variant,
    type = _ref.type,
    _ref$partials = _ref.partials,
    Root = _ref$partials.Root,
    Checkmark = _ref$partials.Checkmark,
    CheckmarkWrapper = _ref$partials.CheckmarkWrapper,
    rest = _objectWithoutProperties(_ref, _excluded);
  var id = useExternalId("".concat(type, "-input"), externalId);
  var localRef = useRef(null);
  var isControlled = defaultChecked === undefined;
  var hasContainer = variant !== CHECKBOX_RADIO_VARIANT.NO_CONTAINER;
  var _useState = useState(defaultChecked !== null && defaultChecked !== void 0 ? defaultChecked : checked),
    _useState2 = _slicedToArray(_useState, 2),
    isChecked = _useState2[0],
    setIsChecked = _useState2[1];

  // Respect external changes to `defaultChecked` and `checked`
  useEffect(function () {
    setIsChecked(defaultChecked !== null && defaultChecked !== void 0 ? defaultChecked : checked);
  }, [defaultChecked, checked]);

  // Handle implicit deselection of radio in collection
  useEffect(function () {
    var handleSelected = function handleSelected(e) {
      var _ref2 = e,
        emittedRef = _ref2.detail.ref;
      var isDifferent = emittedRef !== localRef;
      var isInCollection = emittedRef.current.attributes.name.value === name;
      var isRadio = type === 'radio';
      if (isDifferent && isInCollection && isRadio) {
        setIsChecked(false);
      }
    };
    document.addEventListener('_mod-uk-ds:radio:selected', handleSelected);
    return function () {
      document.removeEventListener('_mod-uk-ds:radio:selected', handleSelected);
    };
  }, [type, name]);
  var handleClick = function handleClick(event) {
    if (event.target !== localRef.current) {
      var _localRef$current;
      (_localRef$current = localRef.current) === null || _localRef$current === void 0 ? void 0 : _localRef$current.click();
    }
  };
  var handleKeyUp = function handleKeyUp(_) {
    var _localRef$current2;
    (_localRef$current2 = localRef.current) === null || _localRef$current2 === void 0 ? void 0 : _localRef$current2.focus();
  };
  var handleOnChange = function handleOnChange(e) {
    setIsChecked(e.currentTarget.checked);
    if (type === 'radio') {
      var event = new CustomEvent('_mod-uk-ds:radio:selected', {
        detail: {
          ref: localRef
        }
      });
      document.dispatchEvent(event);
    }
    if (onChange) {
      onChange(e);
    }
  };
  return /*#__PURE__*/React.createElement(StyledWrapper, null, /*#__PURE__*/React.createElement(Root, {
    className: className,
    $isDisabled: isDisabled,
    $hasContainer: hasContainer,
    $isInvalid: isInvalid,
    $isChecked: isChecked,
    onClick: handleClick,
    onKeyUp: handleKeyUp
  }, /*#__PURE__*/React.createElement(StyledInnerWrapper, {
    $hasContainer: hasContainer
  }, /*#__PURE__*/React.createElement(StyledLabel, {
    $hasContainer: hasContainer,
    $hasDescription: !!description,
    $isDisabled: isDisabled,
    htmlFor: id
  }, /*#__PURE__*/React.createElement(CheckmarkWrapper, {
    $hasContainer: hasContainer
  }, /*#__PURE__*/React.createElement(StyledInput, _extends({
    defaultChecked: defaultChecked,
    ref: mergeRefs([localRef, ref]),
    id: id,
    type: type,
    name: name,
    value: value,
    onChange: handleOnChange,
    onBlur: onBlur,
    disabled: isDisabled,
    checked: isControlled ? isChecked : undefined
  }, rest)), /*#__PURE__*/React.createElement(Checkmark, {
    $hasContainer: hasContainer,
    $isDisabled: isDisabled
  })), label, description && /*#__PURE__*/React.createElement(StyledDescription, null, description)))));
});
CheckboxRadioBase.displayName = 'CheckboxRadioBase';