"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __spreadArrays = (this && this.__spreadArrays) || function () {
    for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
    for (var r = Array(s), k = 0, i = 0; i < il; i++)
        for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
            r[k] = a[j];
    return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.noop = exports.cleanHTML = exports.stringifyLoadResult = exports.waitForRender = exports.waitForError = exports.createMockEngine = exports.TEST_FIXTURE_SRC_DIRECTORY = void 0;
var path = require("path");
var __1 = require("../../");
var paperclip_utils_1 = require("paperclip-utils");
exports.TEST_FIXTURE_SRC_DIRECTORY = path.join(__dirname, "../../test-fixtures/src");
exports.createMockEngine = function (graph, onErr, io) {
    if (onErr === void 0) { onErr = function (e) { return console.error(e); }; }
    if (io === void 0) { io = {}; }
    return __1.createEngine({
        io: __assign({ readFile: function (uri) { return graph[uri]; }, fileExists: function (uri) { return Boolean(graph[uri]); }, resolveFile: function (from, to) {
                return path.join(path.dirname(from), to).replace(/\\/g, "/");
            } }, io)
    }, onErr);
};
exports.waitForError = function (engine, test) {
    if (test === void 0) { test = function () { return true; }; }
    return new Promise(function (resolve) {
        engine.onEvent(function (event) {
            if (event.kind === paperclip_utils_1.EngineDelegateEventKind.Error && test(event)) {
                resolve(event);
            }
        });
    });
};
exports.waitForRender = function (engine, test) {
    if (test === void 0) { test = function () { return true; }; }
    return new Promise(function (resolve) {
        engine.onEvent(function (event) {
            if (event.kind === paperclip_utils_1.EngineDelegateEventKind.Loaded && test(event)) {
                resolve(event);
            }
        });
    });
};
exports.stringifyLoadResult = function (_a) {
    var sheet = _a.sheet, preview = _a.preview, sheets = _a.importedSheets;
    var sheetText = __spreadArrays(sheets.map(function (_a) {
        var sheet = _a.sheet;
        return sheet;
    }), [sheet]).map(function (sheet) {
        return paperclip_utils_1.stringifyCSSSheet(sheet, { protocol: "" });
    })
        .join("\n")
        .trim();
    var buffer = "<style>" + sheetText + "</style>" + paperclip_utils_1.stringifyVirtualNode(preview);
    return exports.cleanHTML(buffer);
};
exports.cleanHTML = function (value) {
    return value.replace(/[\r\n\t\s]+/g, " ").trim();
};
// eslint-disable-next-line
exports.noop = function () { };
