import IllusionEngine from './filter/illusion';
import UnCompress from './utils/uncompress';
var zipUnCompress = new UnCompress();
var parentPath = '/';
/**
 * 滤镜 sdk
 *
 * @note 一般需要创建多个窗口和多个实体
 * 如果有需要，则需要保存下 windowId， entityId，用来销毁和管理资源
 * 一个对于目前的使用场景仅仅需要一个实体
 */
var Illusion = /** @class */ (function () {
    function Illusion() {
        this.canvas = document.createElement('canvas');
    }
    Illusion.prototype.initModule = function () {
        var _this = this;
        return IllusionEngine().then(function (module) {
            window.wasm = module;
            _this.illusionModule = module;
            return module;
        });
    };
    Illusion.prototype.initStage = function (left, top, width, height, zipFilePath, debug) {
        this.illusionModule.initStage(left, top, width + left, height + top, zipFilePath, debug);
    };
    Illusion.prototype.createWindow = function () {
        var windowId = this.illusionModule.createWindow();
        return windowId;
    };
    Illusion.prototype.setWindowRect = function (windowId, left, top, width, height) {
        console.log({ left: left, top: top, right: left + width, bottom: top + height });
        this.illusionModule.setWindowRect(windowId, left, top, left + width, top + height);
    };
    Illusion.prototype.destroyWindow = function (windowId) {
        return this.illusionModule.destroyWindow(windowId);
    };
    Illusion.prototype.createEntity = function (windowId) {
        var entityId = this.illusionModule.createEntity(windowId);
        return entityId;
    };
    Illusion.prototype.setEntityRect = function (entityId, left, top, right, bottom) {
        this.illusionModule.setEntityRect(entityId, left, top, right, bottom);
    };
    Illusion.prototype.setEntityTexture = function (entityId, filePath, rotation, x, y, width, height) {
        if (rotation === void 0) { rotation = 0; }
        if (x === void 0) { x = 0; }
        if (y === void 0) { y = 0; }
        if (width === void 0) { width = 0; }
        if (height === void 0) { height = 0; }
        this.illusionModule.setEntityTexture(entityId, filePath, rotation, x, y, width, height);
    };
    Illusion.prototype.destroyEntity = function (entityId) {
        return this.illusionModule.destroyEntity(entityId);
    };
    Illusion.prototype.clearContext = function () {
        this.illusionModule._clearContexts();
    };
    Illusion.prototype.createWebGLContext = function (width, height, id, parent) {
        var canvas = this.canvas;
        var context = canvas.getContext('webgl2');
        var idBuffer = this.illusionModule._malloc(id.length + 1);
        canvas.id = id;
        canvas.width = width;
        canvas.height = height;
        if (parent) {
            parent.appendChild(context.canvas);
        }
        this.illusionModule.stringToUTF8(id, idBuffer, id.length + 1);
        this.illusionModule._createContext(width, height, idBuffer, 0);
        return context;
    };
    Illusion.prototype.getContext = function () {
        return this.canvas.getContext('2d');
    };
    Illusion.prototype.addEntityFilter = function (entityId, preIndex, filePath, filter) {
        this.illusionModule.addEntityFilter(entityId, preIndex, filePath, filter);
    };
    Illusion.prototype.removeEntityFilter = function (entityId, preIndex) {
        this.illusionModule.removeEntityFilter(entityId, preIndex);
    };
    Illusion.prototype.setEntityFilter = function (entityId, filePath, filter) {
        this.illusionModule.setEntityFilter(entityId, filePath, filter);
        this.renderFrame(0);
    };
    Illusion.prototype.decJsonFile = function (filePath) {
        return this.illusionModule.decJsonFile(filePath);
    };
    Illusion.prototype.setEntityTextureIput = function (entityId, filterIndex, filepath, input) {
        this.illusionModule.setEntityTextureIput(entityId, filterIndex, filepath, input);
    };
    Illusion.prototype.setEntityTextureRect = function (entityId, rect, rgba, input) {
        this.illusionModule.setEntityTextureRect(entityId, rect.left, rect.right, rect.top, rect.bottom, rgba.r, rgba.g, rgba.b, rgba.a, input);
    };
    Illusion.prototype.updateEntityFilter = function (entityId, preIndex, index, filterString) {
        this.illusionModule.updateParamValue(entityId, preIndex.toString(), index.toString(), filterString);
    };
    Illusion.prototype.updateFilterStrongValue = function (entityId, strong) {
        this.illusionModule.updateFilterStrongValue(entityId, strong);
    };
    Illusion.prototype.unCompressZip = function (file) {
        return zipUnCompress.archiveOpenFile(file, '').then(function (archive) {
            return archive.entries;
        });
    };
    Illusion.prototype.createFolder = function (parent, dir) {
        if (parent === void 0) { parent = '/'; }
        try {
            this.illusionModule.FS_createFolder(parent, dir, true, true);
            return parent + "/" + dir;
        }
        catch (err) {
            console.log('create folder failed');
            return null;
        }
    };
    Illusion.prototype.setEntityBlend = function (entityId, fs, vs) {
        this.illusionModule.setEntityBlend(entityId, fs, vs);
    };
    Illusion.prototype.updateBlendParameterValue = function (entityId, json) {
        this.illusionModule.updateBlendParameterValue(entityId, json);
    };
    Illusion.prototype.writeFileToMemory = function (buffer, dir) {
        var int8Array = new Int8Array(buffer);
        var paths = dir.split('/');
        var length = paths.length;
        var fileName = paths[length - 1];
        var path = length > 1 ? paths.slice(0, length - 1).join('/') + "/" : parentPath;
        this.illusionModule.FS_createDataFile(path, fileName, int8Array, true, true, true);
        return "" + path + fileName;
    };
    Illusion.prototype.readFile = function (filePath) {
        return this.illusionModule.readFile(filePath);
    };
    Illusion.prototype.encryptFile = function (srcPath, dstPath) {
        this.illusionModule.encryptFile(srcPath, dstPath);
    };
    Illusion.prototype.setEntityZOrder = function (entityId, order) {
        this.illusionModule.setEntityZOrder(entityId, order);
    };
    Illusion.prototype.setEntityVisible = function (entityId, visible) {
        console.log(visible);
        this.illusionModule.setEntityVisible(entityId, visible);
    };
    Illusion.prototype.destroyAllResource = function () {
        this.illusionModule.destroyAllResource();
    };
    Illusion.prototype.destroy = function () {
        this.illusionModule.shutdown();
    };
    Illusion.prototype.renderFrame = function (currentClockMS) {
        this.illusionModule._renderFrame(currentClockMS);
    };
    return Illusion;
}());
export default Illusion;
