"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../../util/logger");
const sassContext_1 = require("../sassContext");
class SassDataContext {
    /**
     *
     */
    constructor(input, cwrapCtx, interop) {
        this.cwrapCtx = cwrapCtx;
        this.interop = interop;
        this.inputDataPtr = this.interop.str.alloc(input);
        this.sassDataContextPtr = this.cwrapCtx.make_data_context(this.inputDataPtr);
        logger_1.log(`SassDataContext: created new instance`, { sassDataContextPtr: this.sassDataContextPtr });
    }
    //Instead of reconstructing js object from raw pointer,
    //returns reference of option instance already created.
    get options() {
        const sassOptionPtr = this.cwrapCtx.data_context_get_options(this.sassDataContextPtr);
        //internal access to raw pointer - interface doesn't expose it.
        const containedOptionPtr = this.sassOptions.sassOptionsPtr;
        logger_1.log(`SassDataContext: get option pointer from context`, { sassOptionPtr, containedOptionPtr });
        if (!!this.sassOptions && this.sassOptions.sassOptionsPtr === sassOptionPtr) {
            return this.sassOptions;
        }
        logger_1.log(`SassDataContext: pointer mismatch between datacontext to contained value, returning empty`);
        return null;
    }
    set options(option) {
        if (!option) {
            logger_1.log(`SassDataContext: 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.data_context_set_options(this.sassDataContextPtr, this.sassOptions.sassOptionsPtr);
    }
    getContext() {
        const sassContextPtr = this.cwrapCtx.data_context_get_context(this.sassDataContextPtr);
        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_data_context(this.sassDataContextPtr);
    }
    dispose() {
        this.cwrapCtx.delete_data_context(this.sassDataContextPtr);
        this.interop.free(this.inputDataPtr);
    }
}
exports.SassDataContext = SassDataContext;
//# sourceMappingURL=sassDataContext.js.map