var fnz = (function () {
    return {
        getEl: function (id) { return document.getElementById(id); },
        scrollIntoViewById: function (id) { fnz.getEl(id).scrollIntoView(true); },
        blockDownloadAndOpenHtmlPage: function (a) {
            window.open(a.href + '.html', a.target, 1);
            return false;
        },
        downloadFile: function (filename, contentType, content) {
            // Create the URL
            const file = new File([content], filename, { type: contentType });
            const exportUrl = URL.createObjectURL(file);

            // Create the <a> element and click on it
            const a = document.createElement("a");
            document.body.appendChild(a);
            a.href = exportUrl;
            a.download = filename;
            a.target = "_self";
            a.click();

            // We don't need to keep the object URL, let's release the memory
            // On older versions of Safari, it seems you need to comment this line...
            URL.revokeObjectURL(exportUrl);
            a.remove();
        },
        getClientHeight: function (id) { return fnz.getEl(id).clientHeight; },
        setDraggableOn: function (id) { fnz.getEl(id).setAttribute('draggable', 'true') },
        setDraggableOff: function (id) { fnz.getEl(id).removeAttribute('draggable') },
        initDragSource: function (id, dotNetRef, dataType) {
            var el = fnz.getEl(id);
            if (!el || !dotNetRef) return;
            if (!dataType) dataType = "text/plain";

            el.addEventListener('dragstart', async function (ev) {
                var c = await dotNetRef.invokeMethodAsync("FnzDragStartProvideContent", dataType);
                if (c == null) {
                    e.stopPropagation();
                    e.preventDefault();
                    return false;
                }
                else ev.dataTransfer.setData(dataType, c);
            })
        },
        initDropZone: function (id, dotNetRef, dataType) {
            var el = fnz.getEl(id);
            if (!el || !dotNetRef) return;
            if (!dataType) dataType = "text/plain";

            el.addEventListener('dragover',
                async function (ev) {
                    if (-1 != ev.dataTransfer.types.indexOf(dataType)) {
                        ev.dataTransfer.dropEffect = 'move';
                        ev.preventDefault();
                    }
                });

            el.addEventListener('drop',
                async function (ev) {
                    var c = ev.dataTransfer.getData(dataType);
                    if (c == null || c == "") return;
                    await dotNetRef.invokeMethodAsync("FnzDragDropContent", dataType, c);
                    ev.stopImmediatePropagation();
                });
        },
        resolveSelectTextClickConflict: function (id, dotNetRef) {
            var el = fnz.getEl(id);
            if (!el || !dotNetRef) return;

            el.addEventListener('click', (e) => {
                const cellText = document.getSelection();
                if (cellText.type === 'Range') e.stopPropagation();
            })
        }
    }
})();
