import { __extends } from "tslib";
import { PrivateConstructorError } from "../errors";
import PDFObject from "./PDFObject";
import CharCodes from "../syntax/CharCodes";
import { IsIrregular } from "../syntax/Irregular";
import { charFromHexCode, copyStringIntoBuffer, toCharCode, toHexString, } from "../../utils";
var decodeName = function (name) {
    return name.replace(/#([\dABCDEF]{2})/g, function (_, hex) { return charFromHexCode(hex); });
};
var isRegularChar = function (charCode) {
    return charCode >= CharCodes.ExclamationPoint &&
        charCode <= CharCodes.Tilde &&
        !IsIrregular[charCode];
};
var ENFORCER = {};
var pool = new Map();
var PDFName = /** @class */ (function (_super) {
    __extends(PDFName, _super);
    function PDFName(enforcer, name) {
        var _this = this;
        if (enforcer !== ENFORCER)
            throw new PrivateConstructorError('PDFName');
        _this = _super.call(this) || this;
        var encodedName = '/';
        for (var idx = 0, len = name.length; idx < len; idx++) {
            var character = name[idx];
            var code = toCharCode(character);
            encodedName += isRegularChar(code) ? character : "#" + toHexString(code);
        }
        _this.encodedName = encodedName;
        return _this;
    }
    PDFName.prototype.value = function () {
        return this.encodedName;
    };
    PDFName.prototype.clone = function () {
        return this;
    };
    PDFName.prototype.toString = function () {
        return this.encodedName;
    };
    PDFName.prototype.sizeInBytes = function () {
        return this.encodedName.length;
    };
    PDFName.prototype.copyBytesInto = function (buffer, offset) {
        offset += copyStringIntoBuffer(this.encodedName, buffer, offset);
        return this.encodedName.length;
    };
    PDFName.of = function (name) {
        var decodedValue = decodeName(name);
        var instance = pool.get(decodedValue);
        if (!instance) {
            instance = new PDFName(ENFORCER, decodedValue);
            pool.set(decodedValue, instance);
        }
        return instance;
    };
    /* tslint:disable member-ordering */
    PDFName.Length = PDFName.of('Length');
    PDFName.FlateDecode = PDFName.of('FlateDecode');
    PDFName.Resources = PDFName.of('Resources');
    PDFName.Font = PDFName.of('Font');
    PDFName.XObject = PDFName.of('XObject');
    PDFName.Contents = PDFName.of('Contents');
    PDFName.Type = PDFName.of('Type');
    PDFName.Parent = PDFName.of('Parent');
    PDFName.MediaBox = PDFName.of('MediaBox');
    PDFName.Page = PDFName.of('Page');
    PDFName.Annots = PDFName.of('Annots');
    PDFName.TrimBox = PDFName.of('TrimBox');
    PDFName.BleedBox = PDFName.of('BleedBox');
    PDFName.CropBox = PDFName.of('CropBox');
    PDFName.Rotate = PDFName.of('Rotate');
    PDFName.AcroForm = PDFName.of('AcroForm');
    PDFName.Pages = PDFName.of('Pages');
    PDFName.Catalog = PDFName.of('Catalog');
    PDFName.FT = PDFName.of('FT');
    PDFName.Ff = PDFName.of('Ff');
    PDFName.Kids = PDFName.of('Kids');
    PDFName.Fields = PDFName.of('Fields');
    PDFName.Btn = PDFName.of('Btn');
    PDFName.Ch = PDFName.of('Ch');
    PDFName.Tx = PDFName.of('Tx');
    PDFName.Sig = PDFName.of('Sig');
    PDFName.Opt = PDFName.of('Opt');
    PDFName.TI = PDFName.of('TI');
    PDFName.I = PDFName.of('I');
    PDFName.V = PDFName.of('V');
    PDFName.DV = PDFName.of('DV');
    PDFName.Yes = PDFName.of('Yes');
    PDFName.Off = PDFName.of('Off');
    PDFName.RV = PDFName.of('RV');
    PDFName.MaxLen = PDFName.of('MaxLen');
    PDFName.AP = PDFName.of('AP');
    PDFName.N = PDFName.of('N');
    PDFName.P = PDFName.of('P');
    PDFName.NM = PDFName.of('NM');
    PDFName.M = PDFName.of('M');
    PDFName.F = PDFName.of('F');
    PDFName.AS = PDFName.of('AS');
    PDFName.Border = PDFName.of('Border');
    PDFName.C = PDFName.of('C');
    PDFName.StructParent = PDFName.of('StructParent');
    PDFName.OC = PDFName.of('OC');
    PDFName.Subtype = PDFName.of('Subtype');
    PDFName.Text = PDFName.of('Text');
    PDFName.Link = PDFName.of('Link');
    PDFName.FreeText = PDFName.of('FreeText');
    PDFName.Line = PDFName.of('Line');
    PDFName.Square = PDFName.of('Square');
    PDFName.Circle = PDFName.of('Circle');
    PDFName.Polygon = PDFName.of('Polygon');
    PDFName.PolyLine = PDFName.of('PolyLine');
    PDFName.Highlight = PDFName.of('Highlight');
    PDFName.Underline = PDFName.of('Underline');
    PDFName.Squiggly = PDFName.of('Squiggly');
    PDFName.StrikeOut = PDFName.of('StrikeOut');
    PDFName.Stamp = PDFName.of('Stamp');
    PDFName.Caret = PDFName.of('Caret');
    PDFName.Ink = PDFName.of('Ink');
    PDFName.Popup = PDFName.of('Popup');
    PDFName.FileAttachment = PDFName.of('FileAttachment');
    PDFName.Sound = PDFName.of('Sound');
    PDFName.Movie = PDFName.of('Movie');
    PDFName.Widget = PDFName.of('Widget');
    PDFName.Screen = PDFName.of('Screen');
    PDFName.PrinterMark = PDFName.of('PrinterMark');
    PDFName.TrapNet = PDFName.of('TrapNet');
    PDFName.Watermark = PDFName.of('Watermark');
    PDFName.ThreeD = PDFName.of('3D');
    PDFName.Redact = PDFName.of('Redact');
    PDFName.Rect = PDFName.of('Rect');
    PDFName.R = PDFName.of('R');
    PDFName.D = PDFName.of('D');
    PDFName.On = PDFName.of('On');
    PDFName.Lock = PDFName.of('Lock');
    PDFName.SV = PDFName.of('SV');
    return PDFName;
}(PDFObject));
export default PDFName;
//# sourceMappingURL=PDFName.js.map