if (typeof process !== "undefined") {
    require("./test/mockdom");
}

"use strict";

var assert = require("./test/assertions");
var sendKey = require("./test/user").type;
var ace = require("./ace");
var lang = require("./lib/lang");

var editor;

function mouse(type, pos, properties) {
    var target = editor.renderer.getMouseEventTarget();
    var event = new CustomEvent("mouse" + type, {bubbles: true});

    if ("row" in pos) {
        var pagePos = editor.renderer.textToScreenCoordinates(pos.row, pos.column);
        event.clientX = pagePos.pageX;
        event.clientY = pagePos.pageY;
    }
    else {
        target = pos;
        var rect = target.getBoundingClientRect();
        event.clientX = rect.left + rect.width / 2;
        event.clientY = rect.top + rect.height / 2;
    }
    Object.assign(event, properties);
    target.dispatchEvent(event);
}

module.exports = {
    setUp: function () {
        editor = ace.edit(null, {
            value: "999"
        });
        document.body.appendChild(editor.container);
        editor.container.style.height = "200px";
        editor.container.style.width = "300px";
        editor.focus();

    },
    tearDown: function () {
        editor.destroy();
        editor = null;
    },
    "test readOnly Option": async function (done) {
        Array.from(document.querySelectorAll(".ace_editor")).forEach(function (el) {
            if (el != editor.container)
                el.remove();
        });
        var nodes = document.querySelectorAll(".ace_tooltip");
        assert.equal(nodes.length, 1);

        let readOnly = editor.getOption("readOnly");
        assert.equal(editor.$hoverTooltip, null);
        assert.equal(readOnly, false);
        editor.setOption("readOnly", true);
        readOnly = editor.getOption("readOnly");
        assert.equal(readOnly, true);
        sendKey("a");

        await lang.sleep(6);
        assert.equal(editor.getValue(), "999");
        assert.ok(editor.hoverTooltip != null);

        var nodes = document.querySelectorAll(".ace_tooltip");
        assert.equal(nodes.length, 2);
        assert.equal(editor.hoverTooltip.isOpen, true);

        mouse("down", editor.container, {button: 0});

        await lang.sleep(6);
        assert.equal(editor.hoverTooltip.isOpen, false);

        editor.setOption("readOnly", false);
        sendKey("a");

        await lang.sleep(6);
        assert.equal(editor.getValue(), "a999");
        var nodes = document.querySelectorAll(".ace_tooltip");
        assert.equal(nodes.length, 2);
        editor.destroy();
        editor.container.remove();
        var nodes = document.querySelectorAll(".ace_tooltip");
        assert.equal(nodes.length, 0);
        done();
    }
};


require("./test/run")(module);