"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../../util/logger");
class SassImportEntry {
    constructor(cwrapImporter, interop, fnPtrHandler) {
        this.cwrapImporter = cwrapImporter;
        this.interop = interop;
        this.fnPtrHandler = fnPtrHandler;
        /**
         * Hold pointers to internaly allocated strings, to be removed when disposing entry instance
         */
        this.allocatedStringPtr = [];
    }
    makeImport(_importCallback) {
        function boo(_path, _cb, _comp) {
            //noop
        }
        this.callbackPtr = this.fnPtrHandler.add(boo);
        this.sassImportEntryPtr = this.cwrapImporter.make_importer(this.callbackPtr, 0, 0 /* TODO: need way to pass cookie */);
        logger_1.log(`SassImportEntry: created new instance`, { sassImportEntryPtr: this.sassImportEntryPtr });
    }
    makeImporter(rel, abs, source, sourceMap) {
        const [relPtr, absPtr, sourcePtr, sourceMapPtr] = [
            this.interop.str.alloc(rel),
            this.interop.str.alloc(abs),
            this.interop.str.alloc(source),
            this.interop.str.alloc(sourceMap)
        ];
        this.allocatedStringPtr.push(relPtr, absPtr, sourcePtr, sourceMapPtr);
        //make_import_entry internally just calls make_import
        this.sassImportEntryPtr = this.cwrapImporter.make_import(relPtr, absPtr, sourcePtr, sourceMapPtr);
        logger_1.log(`SassImportEntry: created new instance`, { sassImportEntryPtr: this.sassImportEntryPtr });
    }
    get error() {
        const column = this.cwrapImporter.import_get_error_column(this.sassImportEntryPtr);
        const line = this.cwrapImporter.import_get_error_line(this.sassImportEntryPtr);
        const error = this.cwrapImporter.import_get_error_message(this.sassImportEntryPtr);
        return {
            column,
            line,
            message: this.interop.str.ptrToString(error)
        };
    }
    set error({ message, line, column }) {
        const messagePtr = this.interop.str.alloc(message);
        this.allocatedStringPtr.push(messagePtr);
        this.cwrapImporter.import_set_error(this.sassImportEntryPtr, messagePtr, line, column);
    }
    get importPath() {
        const pathPtr = this.cwrapImporter.import_get_imp_path(this.sassImportEntryPtr);
        return this.interop.str.ptrToString(pathPtr);
    }
    get absPath() {
        const pathPtr = this.cwrapImporter.import_get_abs_path(this.sassImportEntryPtr);
        return this.interop.str.ptrToString(pathPtr);
    }
    get source() {
        const sourcePtr = this.cwrapImporter.import_get_source(this.sassImportEntryPtr);
        return this.interop.str.ptrToString(sourcePtr);
    }
    get sourceMap() {
        const sourceMapPtr = this.cwrapImporter.import_get_srcmap(this.sassImportEntryPtr);
        return this.interop.str.ptrToString(sourceMapPtr);
    }
    dispose() {
        this.cwrapImporter.delete_import(this.sassImportEntryPtr);
        this.fnPtrHandler.remove(this.callbackPtr);
        this.allocatedStringPtr.forEach(ptr => this.interop.free(ptr));
        this.allocatedStringPtr.splice(0);
    }
}
exports.SassImportEntry = SassImportEntry;
//# sourceMappingURL=sassImportEntry.js.map