function _class_call_check(instance, Constructor) {
    if (!(instance instanceof Constructor)) {
        throw new TypeError("Cannot call a class as a function");
    }
}
function _defineProperties(target, props) {
    for(var i = 0; i < props.length; i++){
        var descriptor = props[i];
        descriptor.enumerable = descriptor.enumerable || false;
        descriptor.configurable = true;
        if ("value" in descriptor) descriptor.writable = true;
        Object.defineProperty(target, descriptor.key, descriptor);
    }
}
function _create_class(Constructor, protoProps, staticProps) {
    if (protoProps) _defineProperties(Constructor.prototype, protoProps);
    if (staticProps) _defineProperties(Constructor, staticProps);
    return Constructor;
}
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;
}
import { select } from "d3-selection";
import { transition } from "d3-transition";
import { accessor, constant } from "../utils/index.js";
var Image = /*#__PURE__*/ function() {
    "use strict";
    function Image() {
        _class_call_check(this, Image);
        _define_property(this, "_duration", void 0);
        _define_property(this, "_height", void 0);
        _define_property(this, "_id", void 0);
        _define_property(this, "_opacity", void 0);
        _define_property(this, "_pointerEvents", void 0);
        _define_property(this, "_select", void 0);
        _define_property(this, "_url", void 0);
        _define_property(this, "_width", void 0);
        _define_property(this, "_x", void 0);
        _define_property(this, "_y", void 0);
        _define_property(this, "_data", void 0);
        this._duration = 600;
        this._height = accessor("height");
        this._id = accessor("id");
        this._opacity = constant(1);
        this._pointerEvents = constant("auto");
        this._url = accessor("url");
        this._width = accessor("width");
        this._x = accessor("x", 0);
        this._y = accessor("y", 0);
    }
    _create_class(Image, [
        {
            /**
      Renders the current Image to the page. If a *callback* is specified, it will be called once the images are done drawing.
    @param callback Optional callback invoked after rendering completes.
*/ key: "render",
            value: function render(callback) {
                var _this = this;
                if (this._select === void 0) this.select(select("body").append("svg").style("width", "".concat(window.innerWidth, "px")).style("height", "".concat(window.innerHeight, "px")).style("display", "block").node());
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                var images = this._select.selectAll(".d3plus-Image").data(this._data, this._id);
                var enter = images.enter().append("image").attr("class", "d3plus-Image").attr("opacity", 0).attr("width", 0).attr("height", 0).attr("x", function(d, i) {
                    return _this._x(d, i) + _this._width(d, i) / 2;
                }).attr("y", function(d, i) {
                    return _this._y(d, i) + _this._height(d, i) / 2;
                });
                var t = transition().duration(this._duration), that = this, update = enter.merge(images);
                update.attr("xlink:href", this._url).style("pointer-events", this._pointerEvents).transition(t).attr("opacity", this._opacity).attr("width", function(d, i) {
                    return _this._width(d, i);
                }).attr("height", function(d, i) {
                    return _this._height(d, i);
                }).attr("x", function(d, i) {
                    return _this._x(d, i);
                }).attr("y", function(d, i) {
                    return _this._y(d, i);
                }).each(function(d, i) {
                    var image = select(this), link = that._url(d, i);
                    var fullAddress = link.indexOf("http://") === 0 || link.indexOf("https://") === 0;
                    if (!fullAddress || link.indexOf(window.location.hostname) === 0) {
                        var img = new window.Image();
                        img.src = link;
                        img.crossOrigin = "Anonymous";
                        img.onload = function() {
                            var canvas = document.createElement("canvas");
                            canvas.width = img.width;
                            canvas.height = img.height;
                            var context = canvas.getContext("2d");
                            context.drawImage(img, 0, 0);
                            image.attr("xlink:href", canvas.toDataURL("image/png"));
                        };
                    }
                });
                images.exit().transition(t).attr("width", function(d, i) {
                    return _this._width(d, i);
                }).attr("height", function(d, i) {
                    return _this._height(d, i);
                }).attr("x", function(d, i) {
                    return _this._x(d, i);
                }).attr("y", function(d, i) {
                    return _this._y(d, i);
                }).attr("opacity", 0).remove();
                if (callback) setTimeout(callback, this._duration + 100);
                return this;
            }
        },
        {
            key: "data",
            value: function data(_) {
                return arguments.length ? (this._data = _, this) : this._data;
            }
        },
        {
            key: "duration",
            value: function duration(_) {
                return arguments.length ? (this._duration = _, this) : this._duration;
            }
        },
        {
            key: "height",
            value: function height(_) {
                return arguments.length ? (this._height = typeof _ === "function" ? _ : constant(_), this) : this._height;
            }
        },
        {
            key: "id",
            value: function id(_) {
                return arguments.length ? (this._id = _, this) : this._id;
            }
        },
        {
            key: "opacity",
            value: function opacity(_) {
                return arguments.length ? (this._opacity = typeof _ === "function" ? _ : constant(_), this) : this._opacity;
            }
        },
        {
            key: "pointerEvents",
            value: function pointerEvents(_) {
                return arguments.length ? (this._pointerEvents = typeof _ === "function" ? _ : constant(_), this) : this._pointerEvents;
            }
        },
        {
            key: "select",
            value: function select1(_) {
                return arguments.length ? (this._select = select(_), this) : this._select;
            }
        },
        {
            key: "url",
            value: function url(_) {
                return arguments.length ? (this._url = _, this) : this._url;
            }
        },
        {
            key: "width",
            value: function width(_) {
                return arguments.length ? (this._width = typeof _ === "function" ? _ : constant(_), this) : this._width;
            }
        },
        {
            key: "x",
            value: function x(_) {
                return arguments.length ? (this._x = typeof _ === "function" ? _ : constant(_), this) : this._x;
            }
        },
        {
            key: "y",
            value: function y(_) {
                return arguments.length ? (this._y = typeof _ === "function" ? _ : constant(_), this) : this._y;
            }
        }
    ]);
    return Image;
}();
/**
    Creates SVG images based on an array of data.
    @example <caption>a sample row of data</caption>
var data = {"url": "file.png", "width": "100", "height": "50"};
@example <caption>passed to the generator</caption>
new Image().data([data]).render();
@example <caption>creates the following</caption>
<image class="d3plus-Image" opacity="1" href="file.png" width="100" height="50" x="0" y="0"></image>
@example <caption>this is shorthand for the following</caption>
image().data([data])();
@example <caption>which also allows a post-draw callback function</caption>
image().data([data])(function() { alert("draw complete!"); })
*/ export { Image as default };
