'use strict';

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

var _rollupPluginBabelHelpers = require('../../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var cls = require('classnames');
var InformationWrapper_styles = require('../../InformationWrapper.styles.js');
var Hint = require('../Hint/Hint.js');
var InformationWrapper_tokens = require('../../InformationWrapper.tokens.js');
var useOutsideClick = require('../../../../hooks/useOutsideClick.js');

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

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

var LabelComponent = function LabelComponent(_ref) {
  var size = _ref.size,
    hasRequiredIndicator = _ref.hasRequiredIndicator,
    requiredIndicatorPlacement = _ref.requiredIndicatorPlacement,
    optional = _ref.optional,
    optionalText = _ref.optionalText,
    label = _ref.label,
    labelHtmlFor = _ref.labelHtmlFor,
    titleCaption = _ref.titleCaption,
    hintText = _ref.hintText,
    hintTrigger = _ref.hintTrigger,
    hintTargetIcon = _ref.hintTargetIcon,
    hintPlacement = _ref.hintPlacement,
    hintHasArrow = _ref.hintHasArrow,
    hintOffset = _ref.hintOffset,
    hintWidth = _ref.hintWidth,
    hintContentLeft = _ref.hintContentLeft,
    disabled = _ref.disabled;
  var _useState = React.useState(false),
    _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
    isHintVisible = _useState2[0],
    setIsHintVisible = _useState2[1];
  var hintRef = React.useRef(null);
  useOutsideClick.useOutsideClick(function () {
    setIsHintVisible(false);
  }, [hintRef]);
  var innerOptional = Boolean(hasRequiredIndicator ? false : optional);
  var requiredPlacementClass = requiredIndicatorPlacement === 'left' ? InformationWrapper_tokens.classes.requiredAlignLeft : undefined;
  var handleHintShow = function handleHintShow() {
    if (disabled) {
      return;
    }
    setIsHintVisible(true);
  };
  var handleHintHide = function handleHintHide() {
    return setIsHintVisible(false);
  };
  var handleHintClick = function handleHintClick(event) {
    var _hintRef$current;
    if (!hintText || hintTrigger !== 'click' || disabled) {
      return;
    }
    event.stopPropagation();
    var targetIsPopover = event.target === hintRef.current;
    var rootHasTarget = (_hintRef$current = hintRef.current) === null || _hintRef$current === void 0 ? void 0 : _hintRef$current.contains(event.target);
    if (!targetIsPopover && !rootHasTarget) {
      setIsHintVisible(true);
    }
  };
  return /*#__PURE__*/React__default.default.createElement(InformationWrapper_styles.IndicatorWrapper, {
    hasLabel: Boolean(label)
  }, /*#__PURE__*/React__default.default.createElement(InformationWrapper_styles.Label, {
    htmlFor: labelHtmlFor
  }, label), hintText && /*#__PURE__*/React__default.default.createElement(InformationWrapper_styles.HintWrapper, {
    className: cls__default.default(_rollupPluginBabelHelpers.defineProperty({}, InformationWrapper_tokens.classes.withoutLabel, !label)),
    hasCaption: Boolean(titleCaption)
  }, /*#__PURE__*/React__default.default.createElement(Hint.HintComponent, {
    ref: hintRef,
    size: size,
    hintText: hintText,
    hintTrigger: hintTrigger,
    isHintVisible: isHintVisible,
    hintTargetIcon: hintTargetIcon,
    hintPlacement: hintPlacement,
    hintHasArrow: hintHasArrow,
    hintOffset: hintOffset,
    hintWidth: hintWidth,
    hintContentLeft: hintContentLeft,
    withoutLabel: !label,
    handleHintShow: handleHintShow,
    handleHintHide: handleHintHide,
    handleHintClick: handleHintClick
  })), hasRequiredIndicator && /*#__PURE__*/React__default.default.createElement(InformationWrapper_styles.Indicator, {
    className: cls__default.default(requiredPlacementClass, _rollupPluginBabelHelpers.defineProperty({}, InformationWrapper_tokens.classes.withoutLabel, !label)),
    hasCaption: Boolean(titleCaption)
  }), innerOptional && /*#__PURE__*/React__default.default.createElement(InformationWrapper_styles.OptionalText, null, optionalText));
};

exports.LabelComponent = LabelComponent;
