"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "LabelComponent", {
    enumerable: true,
    get: function() {
        return LabelComponent;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default(require("classnames"));
var _hooks = require("../../../../hooks");
var _InformationWrapperstyles = require("../../InformationWrapper.styles");
var _Hint = require("../Hint/Hint");
var _InformationWrappertokens = require("../../InformationWrapper.tokens");
function _array_like_to_array(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 _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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;
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    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 _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(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(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var LabelComponent = function LabelComponent(param) {
    var size = param.size, hasRequiredIndicator = param.hasRequiredIndicator, requiredIndicatorPlacement = param.requiredIndicatorPlacement, optional = param.optional, optionalText = param.optionalText, label = param.label, labelHtmlFor = param.labelHtmlFor, titleCaption = param.titleCaption, hintText = param.hintText, hintTrigger = param.hintTrigger, hintTargetIcon = param.hintTargetIcon, hintPlacement = param.hintPlacement, hintHasArrow = param.hintHasArrow, hintOffset = param.hintOffset, hintWidth = param.hintWidth, hintContentLeft = param.hintContentLeft, disabled = param.disabled;
    var _useState = _sliced_to_array((0, _react.useState)(false), 2), isHintVisible = _useState[0], setIsHintVisible = _useState[1];
    var hintRef = (0, _react.useRef)(null);
    (0, _hooks.useOutsideClick)(function() {
        setIsHintVisible(false);
    }, [
        hintRef
    ]);
    var innerOptional = Boolean(hasRequiredIndicator ? false : optional);
    var requiredPlacementClass = requiredIndicatorPlacement === 'left' ? _InformationWrappertokens.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.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)(_define_property({}, _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, _define_property({}, _InformationWrappertokens.classes.withoutLabel, !label)),
        hasCaption: Boolean(titleCaption)
    }), innerOptional && /*#__PURE__*/ _react.default.createElement(_InformationWrapperstyles.OptionalText, null, optionalText));
};
