"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataTransferType = void 0;
exports.isIosSafariShare = isIosSafariShare;
exports.isFilesOnly = isFilesOnly;
exports.isFilesFromHtml = isFilesFromHtml;
exports.isImageFile = isImageFile;
exports.extractTextContentFromHtml = extractTextContentFromHtml;
exports.findNotEmptyContentPosses = findNotEmptyContentPosses;
exports.trimContent = trimContent;
const prosemirror_model_1 = require("prosemirror-model");
const utils_1 = require("../../markdown/Lists/utils.js");
const utils_2 = require("../../../utils/index.js");
/** Сontains all data formats known to us */
var DataTransferType;
(function (DataTransferType) {
    DataTransferType["Text"] = "text/plain";
    DataTransferType["Html"] = "text/html";
    DataTransferType["Yfm"] = "text/yfm";
    DataTransferType["Rtf"] = "text/rtf";
    DataTransferType["UriList"] = "text/uri-list";
    DataTransferType["VSCodeData"] = "vscode-editor-data";
    DataTransferType["Files"] = "Files";
})(DataTransferType || (exports.DataTransferType = DataTransferType = {}));
function isIosSafariShare({ types }) {
    // if link is copied from ios safari share button,
    // then data transfer only have text/uri-list data type
    return (types.includes(DataTransferType.UriList) &&
        !types.includes(DataTransferType.Text) &&
        !types.includes(DataTransferType.Html));
}
function isFilesOnly({ types }) {
    return types.length === 1 && types[0] === DataTransferType.Files;
}
function isFilesFromHtml({ types }) {
    return (types.length === 2 &&
        types.includes(DataTransferType.Files) &&
        types.includes(DataTransferType.Html));
}
function isImageFile(file) {
    return file.type.startsWith('image/');
}
function extractTextContentFromHtml(html) {
    const element = document.createElement('div');
    element.innerHTML = html;
    element.replaceChildren(...Array.from(element.children).filter((v) => v.nodeName !== 'META'));
    // Look if all nodes are div or span. That they don't have any classname and have only text child.
    if (Array.from(element.children).every(({ nodeName, classList, childNodes }) => {
        return ((nodeName === 'DIV' || nodeName === 'SPAN') &&
            !classList.length &&
            childNodes.length === 1 &&
            (childNodes[0].nodeName === '#text' || childNodes[0].nodeName === 'BR'));
    })) {
        return Array.from(element.children).reduce((a, v) => a + v.textContent + '\n', '');
    }
    return null;
}
function findNotEmptyContentPosses(fragment) {
    let firstNodePos = -1;
    let lastNodePos = -1;
    let firstNotEmptyNodePos = -1;
    let lastNotEmptyNodePos = -1;
    fragment.forEach((contentNode, offset) => {
        if (firstNodePos === -1) {
            firstNodePos = offset;
        }
        lastNodePos = offset + contentNode.nodeSize;
        if (!(0, utils_2.isEmptyString)(contentNode)) {
            if ((0, utils_1.isListNode)(contentNode) || (0, utils_1.isListItemNode)(contentNode)) {
                const [start, end] = findNotEmptyContentPosses(contentNode.content);
                if (firstNotEmptyNodePos === -1 && start !== -1) {
                    firstNotEmptyNodePos = offset + start + 1;
                }
                if (end !== -1) {
                    lastNotEmptyNodePos = offset + end + 1;
                }
            }
            else {
                if (firstNotEmptyNodePos === -1) {
                    firstNotEmptyNodePos = offset;
                }
                lastNotEmptyNodePos = offset + contentNode.nodeSize;
            }
        }
    });
    return [firstNotEmptyNodePos, lastNotEmptyNodePos, firstNodePos, lastNodePos];
}
function trimContent(fragment, creatEmptyFragment) {
    const [notEmptyStart, notEmptyEnd, start, end] = findNotEmptyContentPosses(fragment);
    if (notEmptyStart === start && notEmptyEnd === end) {
        return fragment;
    }
    else if (notEmptyStart === -1 && notEmptyEnd === -1) {
        return creatEmptyFragment ? creatEmptyFragment() : prosemirror_model_1.Fragment.empty;
    }
    return fragment.cut(notEmptyStart, notEmptyEnd);
}
//# sourceMappingURL=utils.js.map
