"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var nbind = require("nbind");
var asmjs_1 = require("./asmjs");
var platform = 'asmjs';
var binding;
if (nbind.init) {
    binding = nbind.init('dist/' + platform);
}
else {
    var Module = {
        TOTAL_MEMORY: 16 * 1024 * 1024 * 2,
    };
    binding = asmjs_1.init(nbind, Module);
}
exports.lib = binding.lib;
var OnigCaptureIndexImpl = (function () {
    function OnigCaptureIndexImpl(index, start, end, length) {
        this.index = index;
        this.start = start;
        this.end = end;
        this.length = length;
    }
    OnigCaptureIndexImpl.prototype.fromJS = function (output) {
        output(this.index, this.start, this.end, this.length);
    };
    return OnigCaptureIndexImpl;
}());
exports.OnigCaptureIndexImpl = OnigCaptureIndexImpl;
binding.bind('OnigCaptureIndex', OnigCaptureIndexImpl);
var OnigNextMatchResult = (function () {
    function OnigNextMatchResult(noMatch, index, captureIndices) {
        this.noMatch = noMatch;
        this.index = index;
        this.captureIndices = captureIndices;
    }
    OnigNextMatchResult.prototype.fromJS = function (output) {
        output(this.noMatch, this.index, this.captureIndices);
    };
    return OnigNextMatchResult;
}());
binding.bind('OnigNextMatchResult', OnigNextMatchResult);
var OnigRegExp = (function () {
    function OnigRegExp(source) {
        this.scanner = new exports.OnigScanner([source]);
    }
    OnigRegExp.captureIndicesForMatch = function (s, match) {
        if (match) {
            var captureIndices = match.captureIndices;
            for (var _i = 0, _a = Array.from(captureIndices); _i < _a.length; _i++) {
                var capture = _a[_i];
                capture.match = s.slice(capture.start, capture.end);
            }
            return captureIndices;
        }
        return null;
    };
    OnigRegExp.prototype.searchSync = function (s, startPosition) {
        if (startPosition === void 0) { startPosition = 0; }
        var onigString = convertToOnigString(s);
        var match = this.scanner.findNextMatchSync(s, startPosition);
        onigString.dispose();
        if (match) {
            return OnigRegExp.captureIndicesForMatch(s, match);
        }
        return null;
    };
    OnigRegExp.prototype.testSync = function (s) {
        return this.searchSync(s) != null;
    };
    return OnigRegExp;
}());
exports.OnigRegExp = OnigRegExp;
exports.OnigScanner = exports.lib.OnigScanner;
exports.OnigScanner.prototype.findNextMatchSync = function (s, startPosition) {
    if (startPosition === void 0) { startPosition = 0; }
    var os = convertToOnigString(s);
    var result = this.FindNextMatchSync(os, convertToPositiveCountableInteger(startPosition));
    if (typeof s === 'string') {
        os.free();
    }
    if (result.noMatch) {
        return null;
    }
    return result;
};
exports.OnigScanner.prototype._findNextMatchSync = exports.OnigScanner.prototype.findNextMatchSync;
exports.OnigScanner.prototype.dispose = exports.OnigScanner.prototype.free;
function convertToPositiveCountableInteger(value) {
    value = parseInt(value, 10);
    if (!isFinite(value)) {
        value = 0;
    }
    value = Math.max(value, 0);
    return value;
}
function convertToOnigString(value) {
    if (value instanceof OnigString) {
        return value;
    }
    if (typeof value === 'string') {
        return new OnigString(value ? value.toString() : value);
    }
    throw new Error('OnigString: invalid value: ' + typeof value);
}
var OnigString = (function (_super) {
    __extends(OnigString, _super);
    function OnigString(value) {
        return _super.call(this, encodeStringAsUTF16Array(value)) || this;
    }
    return OnigString;
}(exports.lib.OnigString));
exports.OnigString = OnigString;
Object.defineProperty(exports.lib.OnigString.prototype, 'length', {
    get: function () {
        return this.utf8_length();
    },
});
OnigString.prototype.slice = function (start, end) {
    return this.toString().slice(start, end);
};
OnigString.prototype.substring = function (start, end) {
    return this.toString().substring(start, end);
};
OnigString.prototype.toString = function () {
    return this.utf8_value();
};
OnigString.prototype.dispose = OnigString.prototype.free;
function encodeStringAsUTF16Array(str) {
    if (typeof str !== 'string') {
        return str;
    }
    var buf = new ArrayBuffer(str.length * 2);
    var bufView = new Uint16Array(buf);
    for (var i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
    }
    return buf;
}
//# sourceMappingURL=oniguruma.js.map