"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../../util/logger");
const sassContext_1 = require("../sassContext");
/**
 * @internal
 */
class SassFileContext {
    constructor(inputPath, cwrapCtx, interop) {
        this.cwrapCtx = cwrapCtx;
        this.interop = interop;
        this.inputPathPtr = this.interop.str.alloc(inputPath);
        this.sassFileContextPtr = this.cwrapCtx.make_file_context(this.inputPathPtr);
        logger_1.log(`SassFileContext: created new instance`, { sassFileContextPtr: this.sassFileContextPtr });
    }
    //Instead of reconstructing js object from raw pointer,
    //returns reference of option instance already created.
    get options() {
        const sassOptionPtr = this.cwrapCtx.file_context_get_options(this.sassFileContextPtr);
        //internal access to raw pointer - interface doesn't expose it.
        const containedOptionPtr = this.sassOptions.sassOptionsPtr;
        logger_1.log(`SassFileContext: get option pointer from context`, { sassOptionPtr, containedOptionPtr });
        if (!!this.sassOptions && this.sassOptions.sassOptionsPtr === sassOptionPtr) {
            return this.sassOptions;
        }
        logger_1.log(`SassFileContext: pointer mismatch between datacontext to contained value, returning empty`);
        return null;
    }
    set options(option) {
        if (!option) {
            logger_1.log(`SassFileContext: cannot set null value as option`);
            return;
        }
        //Hold reference to SassOptionsInterface to access struct values in JS context
        this.sassOptions = null;
        this.sassOptions = option;
        //internal access to raw pointer - interface doesn't expose it.
        this.cwrapCtx.file_context_set_options(this.sassFileContextPtr, this.sassOptions.sassOptionsPtr);
    }
    getContext() {
        const sassContextPtr = this.cwrapCtx.file_context_get_context(this.sassFileContextPtr);
        if (!!this.sassContext && this.sassContext.sassContextPtr !== sassContextPtr) {
            throw new Error(`Unexpected: context has changed`);
        }
        return !!this.sassContext
            ? this.sassContext
            : (this.sassContext = new sassContext_1.SassContext(sassContextPtr, this.cwrapCtx, this.interop.str));
    }
    compile() {
        return this.cwrapCtx.compile_file_context(this.sassFileContextPtr);
    }
    dispose() {
        this.cwrapCtx.delete_file_context(this.sassFileContextPtr);
        this.interop.free(this.inputPathPtr);
    }
}
exports.SassFileContext = SassFileContext;
//# sourceMappingURL=sassFileContext.js.map