/**
 * DevExtreme (renovation/viz/core/renderers/svg_text.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.TextSvgElement = TextSvgElement;
exports.TextSvgElementProps = exports.viewFunction = void 0;
var _base_graphics_props = _interopRequireDefault(require("./base_graphics_props"));
var _utils = require("./utils");
var _type = require("../../../../core/utils/type");
var _config_context = require("../../../common/config_context");
var Preact = _interopRequireWildcard(require("preact"));
var _hooks = require("preact/hooks");

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 _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

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 _toConsumableArray(arr) {
    return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) {
    if ("undefined" !== typeof Symbol && Symbol.iterator in Object(iter)) {
        return Array.from(iter)
    }
}

function _arrayWithoutHoles(arr) {
    if (Array.isArray(arr)) {
        return _arrayLikeToArray(arr)
    }
}

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 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
}

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)
}
var KEY_STROKE = "stroke";
var viewFunction = function(_ref) {
    var isStroked = _ref.isStroked,
        props = _ref.props,
        styles = _ref.styles,
        textAnchor = _ref.textAnchor,
        textItems = _ref.textItems,
        textRef = _ref.textRef;
    var texts = textItems || [];
    var fill = props.fill,
        opacity = props.opacity,
        stroke = props.stroke,
        strokeOpacity = props.strokeOpacity,
        strokeWidth = props.strokeWidth,
        text = props.text,
        x = props.x,
        y = props.y;
    return Preact.h("text", _extends({
        ref: textRef,
        x: x,
        y: y,
        style: styles,
        "text-anchor": textAnchor,
        fill: fill,
        stroke: stroke,
        "stroke-width": strokeWidth,
        "stroke-opacity": strokeOpacity,
        opacity: opacity
    }, (0, _utils.getGraphicExtraProps)(props, x, y)), texts.length ? isStroked && texts.map((function(_ref2, index) {
        var className = _ref2.className,
            style = _ref2.style,
            value = _ref2.value;
        return Preact.h("tspan", {
            key: index,
            style: style,
            className: className
        }, value)
    })) : null, texts.length ? texts.map((function(_ref3, index) {
        var className = _ref3.className,
            style = _ref3.style,
            value = _ref3.value;
        return Preact.h("tspan", {
            key: index,
            style: style,
            className: className
        }, value)
    })) : null, !texts.length && text)
};
exports.viewFunction = viewFunction;
var TextSvgElementProps = _objectSpread(_objectSpread({}, _base_graphics_props.default), {}, {
    text: "",
    x: 0,
    y: 0,
    align: "center",
    encodeHtml: true
});
exports.TextSvgElementProps = TextSvgElementProps;

function TextSvgElement(props) {
    var __textRef = (0, _hooks.useRef)();
    var config = (0, _hooks.useContext)(_config_context.ConfigContext);
    var __styles = (0, _hooks.useCallback)((function() {
        var style = props.styles || {};
        return _objectSpread({
            whiteSpace: "pre"
        }, style)
    }), [props.styles]);
    var __textItems = (0, _hooks.useCallback)((function() {
        var items;
        var parsedHtml;
        var text = props.text;
        if (!text) {
            return
        }
        if (!props.encodeHtml && (/<[a-z][\s\S]*>/i.test(text) || -1 !== text.indexOf("&"))) {
            parsedHtml = (0, _utils.removeExtraAttrs)(text);
            items = (0, _utils.parseHTML)(parsedHtml)
        } else if (/\n/g.test(text)) {
            items = (0, _utils.parseMultiline)(text)
        } else if (__isStroked()) {
            items = [{
                value: text.trim(),
                height: 0
            }]
        }
        return items
    }), [props.text, props.encodeHtml, props.stroke, props.strokeWidth]);
    var __isStroked = (0, _hooks.useCallback)((function() {
        return (0, _type.isDefined)(props.stroke) && (0, _type.isDefined)(props.strokeWidth)
    }), [props.stroke, props.strokeWidth]);
    var __textAnchor = (0, _hooks.useCallback)((function() {
        return (0, _utils.convertAlignmentToAnchor)(props.align, null === config || void 0 === config ? void 0 : config.rtlEnabled)
    }), [props.align, config]);
    var __parseTspanElements = (0, _hooks.useCallback)((function(texts) {
        var items = _toConsumableArray(texts);
        var textElements = __textRef.current.children;
        var strokeLength = !__isStroked() ? 0 : items.length;
        for (var i = 0; i < textElements.length; i++) {
            if (i < strokeLength) {
                items[i].stroke = textElements[i]
            } else {
                items[i % items.length].tspan = textElements[i]
            }
        }
        return items
    }), [props.stroke, props.strokeWidth]);
    var __alignTextNodes = (0, _hooks.useCallback)((function(items) {
        var alignment = props.textsAlignment;
        if (!items || !alignment || "center" === alignment) {
            return
        }
        var direction = "left" === alignment ? -1 : 1;
        var maxTextWidth = Math.max.apply(Math, _toConsumableArray(items.map((function(t) {
            return (0, _utils.getTextWidth)(t)
        }))));
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            var textWidth = (0, _utils.getTextWidth)(item);
            if (0 !== maxTextWidth && maxTextWidth !== textWidth) {
                (0, _utils.setTextNodeAttribute)(item, "dx", direction * (Math.round((maxTextWidth - textWidth) / 2 * 10) / 10))
            }
        }
    }), [props.textsAlignment]);
    var __locateTextNodes = (0, _hooks.useCallback)((function(items) {
        var styles = props.styles,
            x = props.x,
            y = props.y;
        var lineHeight = (0, _utils.getLineHeight)(styles || {});
        var item = items[0];
        (0, _utils.setTextNodeAttribute)(item, "x", x);
        (0, _utils.setTextNodeAttribute)(item, "y", y);
        for (var i = 1, ii = items.length; i < ii; ++i) {
            item = items[i];
            if (item.height >= 0) {
                (0, _utils.setTextNodeAttribute)(item, "x", x);
                var height = (0, _utils.getItemLineHeight)(item, lineHeight);
                (0, _utils.setTextNodeAttribute)(item, "dy", height)
            }
        }
    }), [props.styles, props.x, props.y]);
    var __strokeTextNodes = (0, _hooks.useCallback)((function(items) {
        if (!__isStroked()) {
            return
        }
        var stroke = props.stroke,
            strokeWidth = props.strokeWidth;
        var strokeOpacity = props.strokeOpacity || 1;
        var tspan;
        for (var i = 0, ii = items.length; i < ii; ++i) {
            tspan = items[i].stroke;
            tspan.setAttribute(KEY_STROKE, stroke);
            tspan.setAttribute("stroke-width", strokeWidth.toString());
            tspan.setAttribute("stroke-opacity", strokeOpacity.toString());
            tspan.setAttribute("stroke-linejoin", "round")
        }
    }), [props.stroke, props.strokeWidth, props.strokeOpacity]);
    var __restAttributes = (0, _hooks.useCallback)((function() {
        props.align, props.className, props.dashStyle, props.encodeHtml, props.fill, props.opacity, props.rotate, props.rotateX, props.rotateY, props.scaleX, props.scaleY, props.sharp, props.sharpDirection, props.stroke, props.strokeOpacity, props.strokeWidth, props.styles, props.text, props.textsAlignment, props.translateX, props.translateY, props.x, props.y;
        var restProps = _objectWithoutProperties(props, ["align", "className", "dashStyle", "encodeHtml", "fill", "opacity", "rotate", "rotateX", "rotateY", "scaleX", "scaleY", "sharp", "sharpDirection", "stroke", "strokeOpacity", "strokeWidth", "styles", "text", "textsAlignment", "translateX", "translateY", "x", "y"]);
        return restProps
    }), [props]);
    (0, _hooks.useEffect)((function() {
        var texts = __textItems();
        if (texts) {
            var items = __parseTspanElements(texts);
            __alignTextNodes(items);
            if (void 0 !== props.x || void 0 !== props.y) {
                __locateTextNodes(items)
            }
            __strokeTextNodes(items)
        }
    }), [props.text, props.encodeHtml, props.stroke, props.strokeWidth, props.textsAlignment, props.x, props.y, props.styles, props.strokeOpacity]);
    return viewFunction({
        props: _objectSpread({}, props),
        textRef: __textRef,
        config: config,
        styles: __styles(),
        textItems: __textItems(),
        isStroked: __isStroked(),
        textAnchor: __textAnchor(),
        parseTspanElements: __parseTspanElements,
        alignTextNodes: __alignTextNodes,
        locateTextNodes: __locateTextNodes,
        strokeTextNodes: __strokeTextNodes,
        restAttributes: __restAttributes()
    })
}
TextSvgElement.defaultProps = _objectSpread({}, TextSvgElementProps);
