"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NativeInstance = void 0;
class NativeInstance {
    constructor(instance, disposeMethod) {
        this.instance = instance;
        this.disposeMethod = disposeMethod;
    }
    get _instance() {
        if (this.instance > 0)
            return this.instance;
        if (this.instance === -1)
            this._instanceNotInitialized();
        throw new Error('instance is disposed');
    }
    set _instance(instance) {
        this.disposeInstance(this.instance);
        this.instance = instance;
    }
    dispose() {
        this.instance = this.disposeInstance(this.instance);
    }
    _setInstance(instance, exception) {
        exception.check(() => {
            this.dispose();
            this.instance = instance;
        }, () => {
            this.disposeInstance(instance);
        });
    }
    _instanceNotInitialized() {
        throw new Error('instance is not initialized');
    }
    disposeInstance(instance) {
        if (instance > 0)
            this.disposeMethod(instance);
        return 0;
    }
}
exports.NativeInstance = NativeInstance;
