/**
 * DevExtreme (renovation/viz/core/tooltip.js)
 * Version: 21.1.0 (build 21016-0316)
 * Build date: Sat Jan 16 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";

function _typeof(obj) {
    if ("function" === typeof Symbol && "symbol" === typeof Symbol.iterator) {
        _typeof = function(obj) {
            return typeof obj
        }
    } else {
        _typeof = function(obj) {
            return obj && "function" === typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
        }
    }
    return _typeof(obj)
}
exports.Tooltip = exports.TooltipProps = exports.viewFunction = void 0;
var _combine_classes = require("../../utils/combine_classes");
var _svg_path = require("./renderers/svg_path");
var _svg_text = require("./renderers/svg_text");
var _shadow_filter = require("./renderers/shadow_filter");
var _utils = require("./renderers/utils");
var _svg_root = require("./renderers/svg_root");
var _type = require("../../../core/utils/type");
var _tooltip_utils = require("./common/tooltip_utils");
var _utils2 = require("../common/utils");
var _utils3 = require("../../../viz/core/utils");
var Preact = _interopRequireWildcard(require("preact"));
var _hooks = require("preact/hooks");
var _compat = require("preact/compat");

function _getRequireWildcardCache() {
    if ("function" !== typeof WeakMap) {
        return null
    }
    var cache = new WeakMap;
    _getRequireWildcardCache = function() {
        return cache
    };
    return cache
}

function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj
    }
    if (null === obj || "object" !== _typeof(obj) && "function" !== typeof obj) {
        return {
            default: obj
        }
    }
    var cache = _getRequireWildcardCache();
    if (cache && cache.has(obj)) {
        return cache.get(obj)
    }
    var newObj = {};
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for (var key in obj) {
        if (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 _objectWithoutProperties(source, excluded) {
    if (null == source) {
        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 (null == source) {
        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
}

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 ("string" === typeof o) {
        return _arrayLikeToArray(o, minLen)
    }
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if ("Object" === n && o.constructor) {
        n = o.constructor.name
    }
    if ("Map" === n || "Set" === n) {
        return Array.from(o)
    }
    if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
        return _arrayLikeToArray(o, minLen)
    }
}

function _arrayLikeToArray(arr, len) {
    if (null == len || 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) {
    if ("undefined" === typeof Symbol || !(Symbol.iterator in Object(arr))) {
        return
    }
    var _arr = [];
    var _n = true;
    var _d = false;
    var _e = void 0;
    try {
        for (var _s, _i = arr[Symbol.iterator](); !(_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 && null != _i.return) {
                _i.return()
            }
        } finally {
            if (_d) {
                throw _e
            }
        }
    }
    return _arr
}

function _arrayWithHoles(arr) {
    if (Array.isArray(arr)) {
        return arr
    }
}

function _extends() {
    _extends = Object.assign || 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 ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter((function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable
            }))
        }
        keys.push.apply(keys, symbols)
    }
    return keys
}

function _objectSpread(target) {
    for (var i = 1; i < arguments.length; i++) {
        var source = null != arguments[i] ? arguments[i] : {};
        if (i % 2) {
            ownKeys(Object(source), true).forEach((function(key) {
                _defineProperty(target, key, source[key])
            }))
        } else if (Object.getOwnPropertyDescriptors) {
            Object.defineProperties(target, Object.getOwnPropertyDescriptors(source))
        } else {
            ownKeys(Object(source)).forEach((function(key) {
                Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key))
            }))
        }
    }
    return target
}

function _defineProperty(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
}
var viewFunction = function(_ref) {
    var border = _ref.border,
        cloudRef = _ref.cloudRef,
        cloudSize = _ref.cloudSize,
        correctedCoordinates = _ref.correctedCoordinates,
        cssClassName = _ref.cssClassName,
        customizedOptions = _ref.customizedOptions,
        filterId = _ref.filterId,
        htmlRef = _ref.htmlRef,
        pointerEvents = _ref.pointerEvents,
        _ref$props = _ref.props,
        arrowWidth = _ref$props.arrowWidth,
        TooltipTemplate = _ref$props.contentTemplate,
        cornerRadius = _ref$props.cornerRadius,
        data = _ref$props.data,
        font = _ref$props.font,
        interactive = _ref$props.interactive,
        opacity = _ref$props.opacity,
        rtl = _ref$props.rtl,
        shadow = _ref$props.shadow,
        visible = _ref$props.visible,
        zIndex = _ref$props.zIndex,
        textRef = _ref.textRef,
        textSize = _ref.textSize,
        textSizeWithPaddings = _ref.textSizeWithPaddings;
    if (!visible || !correctedCoordinates) {
        return Preact.h("div", null)
    }
    var angle = (0, _tooltip_utils.getCloudAngle)(textSizeWithPaddings, correctedCoordinates);
    var d = (0, _tooltip_utils.getCloudPoints)(textSizeWithPaddings, correctedCoordinates, angle, {
        cornerRadius: cornerRadius,
        arrowWidth: arrowWidth
    }, true);
    var styles = interactive ? {
        msUserSelect: "text",
        MozUserSelect: "auto",
        WebkitUserSelect: "auto"
    } : {};
    return Preact.h("div", {
        className: cssClassName,
        style: {
            position: "absolute",
            pointerEvents: "none",
            left: cloudSize.x,
            top: cloudSize.y,
            zIndex: zIndex
        }
    }, Preact.h(_svg_root.RootSvgElement, {
        width: cloudSize.width,
        height: cloudSize.height,
        styles: _objectSpread({
            position: "absolute"
        }, styles)
    }, Preact.h("defs", null, Preact.h(_shadow_filter.ShadowFilter, {
        id: filterId,
        x: "-50%",
        y: "-50%",
        width: "200%",
        height: "200%",
        blur: shadow.blur,
        color: shadow.color,
        offsetX: shadow.offsetX,
        offsetY: shadow.offsetY,
        opacity: shadow.opacity
    })), Preact.h("g", {
        filter: (0, _utils.getFuncIri)(filterId),
        ref: cloudRef,
        transform: "translate(".concat(-cloudSize.x, ", ").concat(-cloudSize.y, ")")
    }, Preact.h(_svg_path.PathSvgElement, {
        pointerEvents: pointerEvents,
        d: d,
        fill: customizedOptions.color,
        stroke: customizedOptions.borderColor,
        strokeWidth: border.strokeWidth,
        strokeOpacity: border.strokeOpacity,
        dashStyle: border.dashStyle,
        opacity: opacity,
        rotate: angle,
        rotateX: correctedCoordinates.x,
        rotateY: correctedCoordinates.y
    }), customizedOptions.html || TooltipTemplate ? null : Preact.h("g", {
        "text-anchor": "middle",
        ref: textRef,
        transform: "translate(".concat(correctedCoordinates.x, ", ").concat(correctedCoordinates.y - textSize.height / 2 - textSize.y, ")")
    }, Preact.h(_svg_text.TextSvgElement, {
        text: customizedOptions.text,
        styles: {
            fill: customizedOptions.fontColor,
            fontFamily: font.family,
            fontSize: font.size,
            fontWeight: font.weight,
            opacity: font.opacity,
            pointerEvents: pointerEvents
        }
    })))), !(customizedOptions.html || TooltipTemplate) ? null : Preact.h("div", {
        ref: htmlRef,
        style: {
            position: "relative",
            display: "inline-block",
            left: correctedCoordinates.x - cloudSize.x - textSize.width / 2,
            top: correctedCoordinates.y - cloudSize.y - textSize.height / 2,
            fill: customizedOptions.fontColor,
            fontFamily: font.family,
            fontSize: font.size,
            fontWeight: font.weight,
            opacity: font.opacity,
            pointerEvents: pointerEvents,
            direction: rtl ? "rtl" : "ltr"
        }
    }, TooltipTemplate && TooltipTemplate(_objectSpread({}, data))))
};
exports.viewFunction = viewFunction;
var TooltipProps = {
    color: "#fff",
    border: {
        color: "#d3d3d3",
        width: 1,
        dashStyle: "solid",
        visible: true
    },
    data: {},
    paddingLeftRight: 18,
    paddingTopBottom: 15,
    x: 0,
    y: 0,
    cornerRadius: 0,
    arrowWidth: 20,
    arrowLength: 10,
    offset: 0,
    canvas: {
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
        width: 0,
        height: 0
    },
    font: {
        color: "#232323",
        family: "Segoe UI",
        opacity: 1,
        size: 12,
        weight: 400
    },
    shadow: {
        blur: 2,
        color: "#000",
        offsetX: 0,
        offsetY: 4,
        opacity: .4
    },
    interactive: false,
    enabled: true,
    shared: false,
    location: "center",
    visible: false,
    rtl: false,
    target: {}
};
exports.TooltipProps = TooltipProps;
var getTemplate = function(TemplateProp) {
    return TemplateProp && (TemplateProp.defaultProps ? function(props) {
        return Preact.h(TemplateProp, _extends({}, props))
    } : TemplateProp)
};
var Tooltip = (0, _compat.forwardRef)((function(props, ref) {
    var __cloudRef = (0, _hooks.useRef)();
    var __textRef = (0, _hooks.useRef)();
    var __htmlRef = (0, _hooks.useRef)();
    var _useState = (0, _hooks.useState)((0, _utils.getNextDefsSvgId)()),
        _useState2 = _slicedToArray(_useState, 2),
        __state_filterId = _useState2[0];
    _useState2[1];
    var _useState3 = (0, _hooks.useState)({
            x: 0,
            y: 0,
            width: 0,
            height: 0
        }),
        _useState4 = _slicedToArray(_useState3, 2),
        __state_textSize = _useState4[0],
        __state_setTextSize = _useState4[1];
    var _useState5 = (0, _hooks.useState)({
            x: 0,
            y: 0,
            width: 0,
            height: 0
        }),
        _useState6 = _slicedToArray(_useState5, 2),
        __state_cloudSize = _useState6[0],
        __state_setCloudSize = _useState6[1];
    var _useState7 = (0, _hooks.useState)(void 0),
        _useState8 = _slicedToArray(_useState7, 2),
        __state_d = _useState8[0];
    _useState8[1];
    var _useState9 = (0, _hooks.useState)(void 0),
        _useState10 = _slicedToArray(_useState9, 2),
        __state_currentTarget = _useState10[0],
        __state_setCurrentTarget = _useState10[1];
    var __textSizeWithPaddings = (0, _hooks.useCallback)((function() {
        var paddingLeftRight = props.paddingLeftRight,
            paddingTopBottom = props.paddingTopBottom;
        return {
            width: __state_textSize.width + 2 * paddingLeftRight,
            height: __state_textSize.height + 2 * paddingTopBottom
        }
    }), [props.paddingLeftRight, props.paddingTopBottom, __state_textSize]);
    var __border = (0, _hooks.useCallback)((function() {
        var border = props.border;
        if (border.visible) {
            return {
                stroke: border.color,
                strokeWidth: border.width,
                strokeOpacity: border.opacity,
                dashStyle: border.dashStyle
            }
        }
        return {}
    }), [props.border]);
    var __customizedOptions = (0, _hooks.useCallback)((function() {
        var border = props.border,
            color = props.color,
            customizeTooltip = props.customizeTooltip,
            data = props.data,
            font = props.font;
        return (0, _tooltip_utils.prepareData)(data, color, border, font, customizeTooltip)
    }), [props.border, props.color, props.customizeTooltip, props.data, props.font]);
    var __margins = (0, _hooks.useCallback)((function() {
        var max = Math.max;
        var shadow = props.shadow;
        var xOff = shadow.offsetX;
        var yOff = shadow.offsetY;
        var blur = 2 * shadow.blur + 1;
        return {
            lm: max(blur - xOff, 0),
            rm: max(blur + xOff, 0),
            tm: max(blur - yOff, 0),
            bm: max(blur + yOff, 0)
        }
    }), [props.shadow]);
    var __pointerEvents = (0, _hooks.useCallback)((function() {
        var interactive = props.interactive;
        return interactive ? "auto" : "none"
    }), [props.interactive]);
    var __cssClassName = (0, _hooks.useCallback)((function() {
        var className = props.className;
        var classesMap = _defineProperty({}, String(className), !!className);
        return (0, _combine_classes.combineClasses)(classesMap)
    }), [props.className]);
    var __correctedCoordinates = (0, _hooks.useCallback)((function() {
        var arrowLength = props.arrowLength,
            canvas = props.canvas,
            offset = props.offset,
            x = props.x,
            y = props.y;
        return (0, _tooltip_utils.recalculateCoordinates)({
            canvas: canvas,
            anchorX: x,
            anchorY: y,
            size: __textSizeWithPaddings(),
            offset: offset,
            arrowLength: arrowLength
        })
    }), [props.arrowLength, props.canvas, props.offset, props.x, props.y, props.paddingLeftRight, props.paddingTopBottom, __state_textSize]);
    var __restAttributes = (0, _hooks.useCallback)((function() {
        props.argumentFormat, props.arrowLength, props.arrowWidth, props.border, props.canvas, props.className, props.color, props.contentTemplate, props.cornerRadius, props.customizeTooltip, props.data, props.enabled, props.font, props.format, props.interactive, props.location, props.offset, props.onTooltipHidden, props.onTooltipShown, props.opacity, props.paddingLeftRight, props.paddingTopBottom, props.rtl, props.shadow, props.shared, props.target, props.visible, props.x, props.y, props.zIndex;
        var restProps = _objectWithoutProperties(props, ["argumentFormat", "arrowLength", "arrowWidth", "border", "canvas", "className", "color", "contentTemplate", "cornerRadius", "customizeTooltip", "data", "enabled", "font", "format", "interactive", "location", "offset", "onTooltipHidden", "onTooltipShown", "opacity", "paddingLeftRight", "paddingTopBottom", "rtl", "shadow", "shared", "target", "visible", "x", "y", "zIndex"]);
        return restProps
    }), [props]);
    var __formatValue = (0, _hooks.useCallback)((function(value, specialFormat) {
        var argumentFormat = props.argumentFormat,
            format = props.format;
        return (0, _utils2.getFormatValue)(value, specialFormat, {
            format: format,
            argumentFormat: argumentFormat
        })
    }), [props.argumentFormat, props.format]);
    var __isEnabled = (0, _hooks.useCallback)((function() {
        return props.enabled
    }), [props.enabled]);
    var __isShared = (0, _hooks.useCallback)((function() {
        return props.shared
    }), [props.shared]);
    var __getLocation = (0, _hooks.useCallback)((function() {
        return (0, _utils3.normalizeEnum)(props.location)
    }), [props.location]);
    (0, _hooks.useEffect)((function() {
        var htmlText = __customizedOptions().html;
        if (htmlText && props.visible) {
            __htmlRef.current.innerHTML = htmlText
        }
    }), [props.border, props.color, props.customizeTooltip, props.data, props.font, props.visible]);
    (0, _hooks.useEffect)((function() {
        if (props.visible) {
            __state_setTextSize((function(__state_textSize) {
                return __textRef.current ? __textRef.current.getBBox() : __htmlRef.current.getBoundingClientRect()
            }))
        }
    }), [props.visible]);
    (0, _hooks.useEffect)((function() {
        if ((0, _type.isDefined)(props.x) && (0, _type.isDefined)(props.y) && props.visible && __cloudRef.current) {
            var size = __cloudRef.current.getBBox();
            var _margins = __margins(),
                bm = _margins.bm,
                lm = _margins.lm,
                rm = _margins.rm,
                tm = _margins.tm;
            __state_setCloudSize((function(__state_cloudSize) {
                return {
                    x: Math.floor(size.x - lm),
                    y: Math.floor(size.y - tm),
                    width: size.width + lm + rm,
                    height: size.height + tm + bm
                }
            }))
        }
    }), [props.x, props.y, props.visible]);
    (0, _hooks.useEffect)((function() {
        var onTooltipHidden = props.onTooltipHidden,
            onTooltipShown = props.onTooltipShown,
            target = props.target,
            visible = props.visible;
        var triggerTooltipHidden = function() {
            if (__state_currentTarget && onTooltipHidden) {
                onTooltipHidden({
                    target: __state_currentTarget
                })
            }
        };
        if (visible && __correctedCoordinates() && __state_currentTarget !== target) {
            triggerTooltipHidden();
            null === onTooltipShown || void 0 === onTooltipShown ? void 0 : onTooltipShown({
                target: target
            });
            __state_setCurrentTarget((function(__state_currentTarget) {
                return target
            }))
        }
        if (!visible) {
            triggerTooltipHidden();
            __state_setCurrentTarget((function(__state_currentTarget) {
                return
            }))
        }
    }), [props.onTooltipHidden, props.onTooltipShown, props.target, props.visible, __state_currentTarget, props.arrowLength, props.canvas, props.offset, props.x, props.y, props.paddingLeftRight, props.paddingTopBottom, __state_textSize]);
    (0, _hooks.useImperativeHandle)(ref, (function() {
        return {
            formatValue: __formatValue,
            isEnabled: __isEnabled,
            isShared: __isShared,
            getLocation: __getLocation
        }
    }), [__formatValue, __isEnabled, __isShared, __getLocation]);
    return viewFunction({
        props: _objectSpread(_objectSpread({}, props), {}, {
            contentTemplate: getTemplate(props.contentTemplate)
        }),
        filterId: __state_filterId,
        textSize: __state_textSize,
        cloudSize: __state_cloudSize,
        d: __state_d,
        currentTarget: __state_currentTarget,
        cloudRef: __cloudRef,
        textRef: __textRef,
        htmlRef: __htmlRef,
        textSizeWithPaddings: __textSizeWithPaddings(),
        border: __border(),
        customizedOptions: __customizedOptions(),
        margins: __margins(),
        pointerEvents: __pointerEvents(),
        cssClassName: __cssClassName(),
        correctedCoordinates: __correctedCoordinates(),
        restAttributes: __restAttributes()
    })
}));
exports.Tooltip = Tooltip;
Tooltip.defaultProps = _objectSpread({}, TooltipProps);
