"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ReservedWords_1 = require("./ReservedWords");
var RESERVED_WORDS_ARRAY = Object.keys(ReservedWords_1.default.WORDS);
var WRAP_PREFIX = '_$';
var WRAP_SUFFIX = '$_';
var VALIDITY_REGEX = /(^[^a-zA-Z_])|([^a-zA-Z_0-9])/g;
function charReplacer(c) {
    return WRAP_PREFIX + c.charCodeAt(0) + WRAP_SUFFIX;
}
function isInt(value) {
    return !isNaN(parseInt(value, 10)) && parseInt(value, 10) === parseFloat(value);
}
function toIdentifier(str) {
    var out = str;
    out = out.replace(VALIDITY_REGEX, charReplacer);
    if (ReservedWords_1.default.WORDS[out]) {
        return "" + WRAP_PREFIX + out + WRAP_SUFFIX;
    }
    if (RESERVED_WORDS_ARRAY.indexOf(out) >= 0) {
        return "" + WRAP_PREFIX + out + WRAP_SUFFIX;
    }
    return out;
}
exports.toIdentifier = toIdentifier;
function toText(str) {
    return str.replace(/_\$([a-zA-Z_0-9]+)\$_/g, function (full) {
        var part = full.slice(2, full.length - 2);
        if (isInt(part)) {
            return String.fromCharCode(parseInt(part, 10));
        }
        return part;
    });
}
exports.toText = toText;
//# sourceMappingURL=JavaScriptIdentifier.js.map