import t from "../../i18n/index.js";
import { isLocalhost } from "../../services/util.js";
import buildUuid from "../../services/uuid.js";
import { snackbar } from "../../user-interface/common/eden-snackbar.js";
import { clipboardError, pasteFromClipboard } from "../block.js";
export function addEditorUuids(editor) {
    if (!editor.withUuid) {
        return;
    }
    const blocks = Array.from(editor.element.querySelectorAll("section[data-template]"));
    blocks.forEach((block) => {
        const uuid = buildUuid();
        block.setAttribute("data-uuid", uuid);
    });
}
export async function pasteEditor(editor) {
    if (navigator && navigator.clipboard) {
        try {
            await pasteFromClipboard("afterbegin", editor.element);
            addEditorUuids(editor);
            editor.desactivate();
            await editor.analyse();
            editor.activate();
            editor.hideUi();
            snackbar(t("snackbars.pasted-content"), "success");
        }
        catch (e) {
            if (isLocalhost()) {
                console.info("Reading from clipboard error", e);
            }
            snackbar(t("snackbars.paste-error"), "error");
        }
    }
    else {
        clipboardError();
    }
}
export async function copyEditor(editor) {
    if (navigator && navigator.clipboard) {
        try {
            editor.desactivate();
            const copy = editor.element.cloneNode(true);
            editor.activate();
            await navigator.clipboard.writeText(`fromEden:${copy.innerHTML}`);
            snackbar(t("snackbars.copied-blocks"), "success");
        }
        catch (e) {
            if (isLocalhost()) {
                console.info("writing into clipboard error", e);
            }
            snackbar(t("snackbars.copy-error"), "error");
        }
    }
    else {
        clipboardError();
    }
}
