"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "LabelComponent", {
    enumerable: true,
    get: function() {
        return LabelComponent;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
const _hooks = require("../../../../hooks");
const _InformationWrapperstyles = require("../../InformationWrapper.styles");
const _Hint = require("../Hint/Hint");
const _InformationWrappertokens = require("../../InformationWrapper.tokens");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const LabelComponent = ({ size, hasRequiredIndicator, requiredIndicatorPlacement, optional, optionalText, label, labelHtmlFor, titleCaption, hintText, hintTrigger, hintTargetIcon, hintPlacement, hintHasArrow, hintOffset, hintWidth, hintContentLeft, disabled })=>{
    const [isHintVisible, setIsHintVisible] = (0, _react.useState)(false);
    const hintRef = (0, _react.useRef)(null);
    (0, _hooks.useOutsideClick)(()=>{
        setIsHintVisible(false);
    }, [
        hintRef
    ]);
    const innerOptional = Boolean(hasRequiredIndicator ? false : optional);
    const requiredPlacementClass = requiredIndicatorPlacement === 'left' ? _InformationWrappertokens.classes.requiredAlignLeft : undefined;
    const handleHintShow = ()=>{
        if (disabled) {
            return;
        }
        setIsHintVisible(true);
    };
    const handleHintHide = ()=>setIsHintVisible(false);
    const handleHintClick = (event)=>{
        if (!hintText || hintTrigger !== 'click' || disabled) {
            return;
        }
        event.stopPropagation();
        const targetIsPopover = event.target === hintRef.current;
        const rootHasTarget = hintRef.current?.contains(event.target);
        if (!targetIsPopover && !rootHasTarget) {
            setIsHintVisible(true);
        }
    };
    return /*#__PURE__*/ _react.default.createElement(_InformationWrapperstyles.IndicatorWrapper, {
        hasLabel: Boolean(label)
    }, /*#__PURE__*/ _react.default.createElement(_InformationWrapperstyles.Label, {
        htmlFor: labelHtmlFor
    }, label), hintText && /*#__PURE__*/ _react.default.createElement(_InformationWrapperstyles.HintWrapper, {
        className: (0, _classnames.default)({
            [_InformationWrappertokens.classes.withoutLabel]: !label
        }),
        hasCaption: Boolean(titleCaption)
    }, /*#__PURE__*/ _react.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.createElement(_InformationWrapperstyles.Indicator, {
        className: (0, _classnames.default)(requiredPlacementClass, {
            [_InformationWrappertokens.classes.withoutLabel]: !label
        }),
        hasCaption: Boolean(titleCaption)
    }), innerOptional && /*#__PURE__*/ _react.default.createElement(_InformationWrapperstyles.OptionalText, null, optionalText));
};
