import { knownFolders, path as filePath } from '@nativescript/core';
import { Helpers } from '../helpers';
let ctor;
// store ref if loading
const loaders = new Map();
export class ImageAsset {
    constructor(native) {
        this._native = native || new global.CanvasModule.ImageAsset();
    }
    get native() {
        return this._native;
    }
    get width() {
        return this.native.width;
    }
    get height() {
        return this.native.height;
    }
    get error() {
        return this.native.error;
    }
    fromUrlSync(url) {
        return this.native.fromUrlSync(url);
    }
    _incrementStrongRef() {
        let count = 0;
        if (loaders.has(this)) {
            count = loaders.get(this);
        }
        count++;
        loaders.set(this, count);
    }
    _decrementStrongRefAndRemove() {
        const count = loaders.get(this) - 1;
        if (count <= 0) {
            loaders.delete(this);
        }
    }
    fromUrl(url) {
        return new Promise((resolve, reject) => {
            this._incrementStrongRef();
            this.native.fromUrlCb(url, (success, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(success);
                }
                this._decrementStrongRefAndRemove();
            });
        });
    }
    fromFileSync(path) {
        let realPath = path;
        if (typeof realPath === 'string') {
            if (realPath.startsWith('~/')) {
                realPath = filePath.join(knownFolders.currentApp().path, realPath.replace('~/', ''));
            }
        }
        return this.native.fromFileSync(realPath);
    }
    fromFile(path) {
        return new Promise((resolve, reject) => {
            if (typeof path === 'string') {
                if (path.startsWith('~/')) {
                    path = filePath.join(knownFolders.currentApp().path, path.replace('~/', ''));
                }
            }
            this._incrementStrongRef();
            this.native.fromFileCb(path, (success, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(success);
                }
                this._decrementStrongRefAndRemove();
            });
        });
    }
    /*

    loadFromNative(image: any): boolean {
        return this.native.loadImageFromImage(image);
    }

    loadFromNative(image: any) {
        return new Promise((resolve, reject) => {
            this.native.loadImageFromImage(
                image,
                new org.nativescript.canvas.TNSImageAsset.Callback({
                    onError(error) {
                        reject(error);
                    },
                    onSuccess(success) {
                        resolve(success);
                    },
                })
            );
        });
    }

    */
    loadFromBytesSync(bytes) {
        return this.native.fromBytesSync(bytes);
    }
    loadFromBytes(bytes) {
        return new Promise((resolve, reject) => {
            this._incrementStrongRef();
            this.native.fromBytesCb(bytes, (success, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(success);
                }
                this._decrementStrongRefAndRemove();
            });
        });
    }
    scale(x, y) {
        this.native.scale(x, y);
    }
    saveSync(path, format) {
        return this.native.saveSync(path, format);
    }
    save(path, format) {
        return new Promise((resolve, reject) => {
            this._incrementStrongRef();
            this.native.saveCb(path, format, (success, error) => {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(success);
                }
                this._decrementStrongRefAndRemove();
            });
        });
    }
    flipX() { }
    flipY() { }
}
(() => {
    Helpers.initialize();
})();
export var ImageAssetSaveFormat;
(function (ImageAssetSaveFormat) {
    ImageAssetSaveFormat[ImageAssetSaveFormat["JPG"] = 0] = "JPG";
    ImageAssetSaveFormat[ImageAssetSaveFormat["PNG"] = 1] = "PNG";
    ImageAssetSaveFormat[ImageAssetSaveFormat["ICO"] = 2] = "ICO";
    ImageAssetSaveFormat[ImageAssetSaveFormat["BMP"] = 3] = "BMP";
    ImageAssetSaveFormat[ImageAssetSaveFormat["TIFF"] = 4] = "TIFF";
})(ImageAssetSaveFormat || (ImageAssetSaveFormat = {}));
//# sourceMappingURL=index.js.map