import { PDFArray, PDFDict, PDFName, PDFNumber, PDFString, } from "../..";
import { PDFNonTerminalField, PDFTerminalField } from './index';
export var acroFormFieldTypes = [
    PDFName.Btn,
    PDFName.Ch,
    PDFName.Tx,
    PDFName.Sig,
];
var PDFAcroField = /** @class */ (function () {
    function PDFAcroField(dict) {
        this.dict = dict;
    }
    PDFAcroField.fromDict = function (dict) {
        var kids = PDFName.Kids;
        var isTerminal = !dict.lookup(kids);
        if (isTerminal) {
            return PDFTerminalField.fromDict(dict);
        }
        return PDFNonTerminalField.fromDict(dict);
    };
    PDFAcroField.prototype.FT = function () {
        return this.lookupMaybeInheritableAttribute(PDFName.FT, PDFName);
    };
    PDFAcroField.prototype.Parent = function () {
        var parentDict = this.dict.lookupMaybe(PDFName.Parent, PDFDict);
        if (!parentDict)
            return undefined;
        return PDFNonTerminalField.fromDict(parentDict);
    };
    PDFAcroField.prototype.Kids = function () {
        return this.dict.lookupMaybe(PDFName.Kids, PDFArray);
    };
    PDFAcroField.prototype.T = function () {
        return this.dict.lookupMaybe(PDFName.of('T'), PDFString);
    };
    PDFAcroField.prototype.TU = function () {
        return this.dict.lookupMaybe(PDFName.of('TU'), PDFString);
    };
    PDFAcroField.prototype.TM = function () {
        return this.dict.lookupMaybe(PDFName.of('TM'), PDFString);
    };
    PDFAcroField.prototype.Ff = function () {
        return this.lookupMaybeInheritableAttribute(PDFName.Ff, PDFNumber);
    };
    PDFAcroField.prototype.getFlags = function () {
        var _a, _b;
        return _b = (_a = this.Ff()) === null || _a === void 0 ? void 0 : _a.value(), (_b !== null && _b !== void 0 ? _b : 0);
    };
    PDFAcroField.prototype.setFlags = function (flags) {
        this.dict.set(PDFName.Ff, PDFNumber.of(flags));
    };
    PDFAcroField.prototype.setFlag = function (flag) {
        var newFlags = this.getFlags() | (1 << flag);
        this.dict.set(PDFName.Ff, PDFNumber.of(newFlags));
    };
    PDFAcroField.prototype.unsetFlag = function (flag) {
        var newFlags = this.getFlags() & ~(1 << flag);
        this.dict.set(PDFName.Ff, PDFNumber.of(newFlags));
    };
    PDFAcroField.prototype.toggleFlag = function (flag) {
        var flagValue = this.getFlags() & (1 << flag);
        if (flagValue) {
            this.unsetFlag(flag);
        }
        else {
            this.setFlag(flag);
        }
    };
    PDFAcroField.prototype.isReadOnly = function () {
        return !!(this.getFlags() & 1);
    };
    PDFAcroField.prototype.isRequired = function () {
        return !!(this.getFlags() & (1 << 1));
    };
    PDFAcroField.prototype.isNoExport = function () {
        return !!(this.getFlags() & (1 << 2));
    };
    PDFAcroField.prototype.setReadOnly = function (readOnly) {
        if (Number(this.isReadOnly()) ^ Number(readOnly)) {
            this.toggleFlag(0);
        }
    };
    PDFAcroField.prototype.setIsRequired = function (required) {
        if (Number(this.isRequired()) ^ Number(required)) {
            this.toggleFlag(1);
        }
    };
    PDFAcroField.prototype.setIsNoExport = function (noExport) {
        if (Number(this.isNoExport()) ^ Number(noExport)) {
            this.toggleFlag(2);
        }
    };
    PDFAcroField.prototype.V = function () {
        return this.getInheritableAttribute(PDFName.V);
    };
    PDFAcroField.prototype.getValue = function () {
        return this.V();
    };
    PDFAcroField.prototype.setValue = function (value) {
        this.dict.set(PDFName.V, value);
    };
    PDFAcroField.prototype.DV = function () {
        return this.getInheritableAttribute(PDFName.DV);
    };
    PDFAcroField.prototype.AA = function () {
        return this.dict.lookupMaybe(PDFName.of('AA'), PDFDict);
    };
    PDFAcroField.prototype.getInheritableAttribute = function (name) {
        var attribute;
        this.ascend(function (node) {
            if (!attribute)
                attribute = node.dict.get(name);
        });
        return attribute;
    };
    PDFAcroField.prototype.lookupMaybeInheritableAttribute = function (name, type) {
        var objectOrRef = this.getInheritableAttribute(name);
        return this.dict.context.lookupMaybe(objectOrRef, type);
    };
    PDFAcroField.prototype.ascend = function (visitor) {
        visitor(this);
        var Parent = this.Parent();
        if (Parent)
            Parent.ascend(visitor);
    };
    return PDFAcroField;
}());
export default PDFAcroField;
//# sourceMappingURL=PDFAcroField.js.map