/**
 * DevExtreme (viz/core/annotations.js)
 * Version: 19.1.0 (build 19071)
 * Build date: Tue Mar 12 2019
 *
 * Copyright (c) 2012 - 2019 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.plugins = exports.__test_utils = exports.createAnnotations = void 0;
var _type = require("../../core/utils/type");
var _extend3 = require("../../core/utils/extend");
var _utils = require("./utils");

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 coreAnnotation(type, options, _draw) {
    return {
        _type: type,
        name: options.name,
        x: options.x,
        y: options.y,
        value: options.value,
        argument: options.argument,
        axis: options.axis,
        draw: function(widget, group) {
            var _widget$_getAnnotatio = widget._getAnnotationCoords(this),
                x = _widget$_getAnnotatio.x,
                y = _widget$_getAnnotatio.y;
            (0, _type.isDefined)(x) && (0, _type.isDefined)(y) && _draw({
                x: x,
                y: y
            }, widget, group)
        }
    }
}

function simpleAnnotation(options) {
    return coreAnnotation("simple", options, function(_ref, widget, group) {
        var x = _ref.x,
            y = _ref.y;
        widget._renderer.circle(x, y, 5).attr({
            fill: "red"
        }).append(group)
    })
}

function labelAnnotation(options) {
    return coreAnnotation("label", options, function(_ref2, widget, group) {
        var x = _ref2.x,
            y = _ref2.y;
        widget._renderer.text(options.label.text, x, y).css((0, _utils.patchFontOptions)(options.label.font)).append(group)
    })
}

function imageAnnotation(options) {
    var _options$image = options.image,
        width = _options$image.width,
        height = _options$image.height,
        url = _options$image.url,
        location = _options$image.location;
    return coreAnnotation("image", options, function(_ref3, widget, group) {
        var x = _ref3.x,
            y = _ref3.y;
        widget._renderer.image(x - .5 * width, y - .5 * height, width, height, url, location).append(group)
    })
}

function mergeOptions(itemOptions, commonOptions, key) {
    return (0, _extend3.extend)(true, {}, itemOptions, _defineProperty({}, key, commonOptions), _defineProperty({}, key, itemOptions[key]))
}

function createAnnotation(itemOptions, commonOptions) {
    if ((0, _type.isDefined)(itemOptions.image)) {
        return imageAnnotation(mergeOptions(itemOptions, commonOptions.imageOptions, "image"))
    } else {
        if ((0, _type.isDefined)(itemOptions.label)) {
            return labelAnnotation(mergeOptions(itemOptions, commonOptions.labelOptions, "label"))
        } else {
            return simpleAnnotation(itemOptions)
        }
    }
}
var createAnnotations = exports.createAnnotations = function(options) {
    return options.items.map(function(itemOptions) {
        return createAnnotation(itemOptions, options)
    })
};
var chartPlugin = {
    name: "annotations_chart",
    init: function() {},
    dispose: function() {},
    members: {
        _getAnnotationCoords: function(annotation) {
            var x = annotation.x;
            var y = annotation.y;
            var argument = annotation.argument;
            var value = annotation.value;
            if (!(0, _type.isDefined)(x) && (0, _type.isDefined)(argument)) {
                x = this.getArgumentAxis().getTranslator().translate(argument)
            }
            if (!(0, _type.isDefined)(y) && (0, _type.isDefined)(value)) {
                var axis = this.getValueAxis(annotation.axis);
                y = axis && axis.getTranslator().translate(value)
            }
            return {
                x: x,
                y: y
            }
        }
    }
};
var corePlugin = {
    name: "annotations_core",
    init: function() {
        this._annotations = {
            items: []
        }
    },
    dispose: function() {
        this._annotationsGroup.linkRemove().linkOff()
    },
    extenders: {
        _createHtmlStructure: function() {
            this._annotationsGroup = this._renderer.g().attr({
                "class": this._rootClassPrefix + "-annotations"
            }).linkOn(this._renderer.root, "annotations").linkAppend()
        },
        _renderExtraElements: function() {
            var _this = this;
            this._annotationsGroup.clear();
            this._annotations.items.forEach(function(item) {
                return item.draw(_this, _this._annotationsGroup)
            })
        }
    },
    members: {
        _buildAnnotations: function() {
            this._annotations.items = [];
            var options = this._getOption("annotations");
            if (!options || !options.items) {
                return
            }
            this._annotations.items = createAnnotations(options)
        },
        _getAnnotationCoords: function() {
            return {}
        }
    },
    customize: function(constructor) {
        constructor.addChange({
            code: "ANNOTATIONS",
            handler: function() {
                this._buildAnnotations();
                this._change(["FORCE_RENDER"])
            },
            isThemeDependent: true,
            isOptionChange: true,
            option: "annotations"
        })
    },
    fontFields: ["annotations.labelOptions.font"]
};
var plugins = exports.plugins = {
    core: corePlugin,
    chart: chartPlugin
};
