/**
 * DevExtreme (cjs/ui/html_editor/modules/dropImage.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _devextremeQuill = _interopRequireDefault(require("devextreme-quill"));
var _events_engine = _interopRequireDefault(require("../../../events/core/events_engine"));
var _index = require("../../../events/utils/index");
var _iterator = require("../../../core/utils/iterator");
var _browser = _interopRequireDefault(require("../../../core/utils/browser"));
var _window = require("../../../core/utils/window");
var _base = _interopRequireDefault(require("./base"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _assertThisInitialized(self) {
    if (void 0 === self) {
        throw new ReferenceError("this hasn't been initialised - super() hasn't been called")
    }
    return self
}

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
let DropImageModule = _base.default;
if (_devextremeQuill.default) {
    DropImageModule = function(_BaseModule) {
        _inheritsLoose(DropImageModule, _BaseModule);

        function DropImageModule(quill, options) {
            var _this;
            _this = _BaseModule.call(this, quill, options) || this;
            const widgetName = _this.editorInstance.NAME;
            _events_engine.default.on(_this.quill.root, (0, _index.addNamespace)("drop", widgetName), _this._dropHandler.bind(_assertThisInitialized(_this)));
            _events_engine.default.on(_this.quill.root, (0, _index.addNamespace)("paste", widgetName), _this._pasteHandler.bind(_assertThisInitialized(_this)));
            return _this
        }
        var _proto = DropImageModule.prototype;
        _proto._dropHandler = function(e) {
            var _dataTransfer$files;
            const dataTransfer = e.originalEvent.dataTransfer;
            const hasFiles = null === dataTransfer || void 0 === dataTransfer ? void 0 : null === (_dataTransfer$files = dataTransfer.files) || void 0 === _dataTransfer$files ? void 0 : _dataTransfer$files.length;
            this.saveValueChangeEvent(e);
            e.preventDefault();
            if (hasFiles) {
                this._getImage(dataTransfer.files, this._addImage.bind(this))
            }
        };
        _proto._pasteHandler = function(e) {
            var _clipboardData$items;
            const {
                clipboardData: clipboardData
            } = e.originalEvent;
            this.saveValueChangeEvent(e);
            if (!clipboardData) {
                return
            }
            const hasDataItems = null === (_clipboardData$items = clipboardData.items) || void 0 === _clipboardData$items ? void 0 : _clipboardData$items.length;
            const isHtmlData = clipboardData.getData("text/html");
            if (!isHtmlData && hasDataItems) {
                this._getImage(clipboardData.items, imageData => {
                    if (_browser.default.mozilla) {
                        return
                    }
                    this._addImage(imageData)
                })
            }
        };
        _proto._isImage = function(file) {
            return !!file.type.match(/^image\/(a?png|bmp|gif|p?jpe?g|svg|vnd\.microsoft\.icon|webp)/i)
        };
        _proto._getImage = function(files, callback) {
            const window = (0, _window.getWindow)();
            (0, _iterator.each)(files, (index, file) => {
                if (!this._isImage(file)) {
                    return
                }
                const reader = new window.FileReader;
                reader.onload = _ref => {
                    let {
                        target: target
                    } = _ref;
                    callback(target.result)
                };
                const readableFile = file.getAsFile ? file.getAsFile() : file;
                if (readableFile instanceof window.Blob) {
                    reader.readAsDataURL(readableFile)
                }
            })
        };
        _proto._addImage = function(data) {
            const selection = this.quill.getSelection();
            const pasteIndex = selection ? selection.index : this.quill.getLength();
            this.quill.insertEmbed(pasteIndex, "extendedImage", data, "user")
        };
        return DropImageModule
    }(_base.default)
}
var _default = DropImageModule;
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;
