"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.CHECKBOX_PLACEMENTS = void 0;
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _truncateString = require("../truncate-string");
var _truncateWithTooltip = require("../truncate-with-tooltip");
var _tooltip = require("../tooltip");
var _truncateString2 = require("../truncate-string/truncate-string");
var _useForwardRef = require("../hooks/use-forward-ref");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
const CHECKBOX_PLACEMENTS = exports.CHECKBOX_PLACEMENTS = {
  TOP: 'top',
  MIDDLE: 'middle'
};
/** Checkboxes are used for a list of options where the user may select multiple options.
 * [CAPITALIZATION] Use sentence case for labels
 * @midnight-gray-supported
 * @alpine-light-supported
 */
const Checkbox = /*#__PURE__*/(0, _react.forwardRef)(({
  id,
  isChecked,
  isDisabled = false,
  isTruncatable = false,
  onChange = () => {},
  onKeyDown = event => event.stopPropagation(),
  labelText = '',
  subtext = '',
  indeterminate = false,
  className,
  testId,
  tooltipDocsLink,
  tooltipText,
  tooltipPlacement,
  placement = CHECKBOX_PLACEMENTS.MIDDLE,
  ariaLabel,
  tabIndex = 0,
  role,
  hasParentTooltip = false
}, ref) => {
  const checkBoxLabelId = !hasParentTooltip ? ['ds-checkbox-label-for', id].filter(Boolean).join('-') : undefined;
  const checkBoxDescriptionId = !hasParentTooltip ? ['ds-checkbox-description-for', id].filter(Boolean).join('-') : undefined;
  const getLabelTextContent = () => {
    if (subtext) {
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
          id: checkBoxLabelId,
          children: labelText
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "checkbox-subtext",
          "test-id": "checkbox-subtext",
          id: checkBoxDescriptionId,
          children: isTruncatable ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateString.TruncateString, {
            text: subtext,
            truncateAt: _truncateString2.ELLIPSIS_PLACE_TYPES.END
          }) : subtext
        })]
      });
    }
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      id: checkBoxLabelId,
      className: (0, _classnames.default)({
        'sr-only': !labelText
      }),
      "test-id": "checkbox-label-text",
      children: labelText
    });
  };
  const getDefaultTooltipText = () => {
    // If subtext is available, we want to retain subtext structure
    // for tooltip.
    if (subtext) {
      return getLabelTextContent();
    }
    return labelText;
  };
  const testSelector = testId || id;
  const label =
  /*#__PURE__*/
  // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-noninteractive-element-interactions
  (0, _jsxRuntime.jsx)("label", {
    className: (0, _classnames.default)(className, {
      'top-placement': placement === CHECKBOX_PLACEMENTS.TOP
    }),
    htmlFor: id,
    "test-id": testSelector,
    onClick: e => e.stopPropagation(),
    "aria-label": ariaLabel,
    children: getLabelTextContent()
  });
  const [setRef, checkboxRef] = (0, _useForwardRef.useForwardRef)(ref);
  (0, _react.useEffect)(() => {
    if (checkboxRef.current) {
      checkboxRef.current.indeterminate = indeterminate;
    }
  }, [indeterminate]);
  const checkBoxAttr = {
    ...(!!ariaLabel && {
      'aria-label': ariaLabel
    }),
    ...(!!labelText && !ariaLabel && {
      'aria-labelledby': checkBoxLabelId
    }),
    ...(!!subtext && {
      'aria-describedby': checkBoxDescriptionId
    })
  };
  const checkboxInput = /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
    type: "checkbox",
    className: "checkbox",
    id: id,
    checked: isChecked,
    disabled: isDisabled,
    onChange: onChange,
    value: String(isChecked),
    "test-id": `${testSelector}-input`,
    ref: setRef,
    onClick: e => e.stopPropagation(),
    onKeyDown: onKeyDown,
    tabIndex: tabIndex,
    ...checkBoxAttr,
    role: role,
    "aria-checked": indeterminate ? 'mixed' : isChecked
  });
  if (!hasParentTooltip) {
    if (isTruncatable) {
      return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "checkbox-wrapper",
        children: [checkboxInput, /*#__PURE__*/(0, _jsxRuntime.jsx)(_truncateWithTooltip.TruncateWithTooltip, {
          tooltipDocsLink: tooltipDocsLink,
          tooltipText: getDefaultTooltipText(),
          tooltipPlacement: tooltipPlacement,
          children: label
        })]
      });
    }
    if (tooltipText) {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
        placement: _tooltip.TOOLTIP_PLACEMENT_TYPES.TOP || tooltipPlacement,
        content: tooltipText,
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
          className: "checkbox-wrapper checkbox-tooltip-container",
          "test-id": "checkbox-tooltip-container",
          children: [checkboxInput, label]
        })
      });
    }
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "checkbox-wrapper",
    children: [checkboxInput, label]
  });
});
Checkbox.displayName = 'Checkbox';
var _default = exports.default = Checkbox;