import { PdfDataExtractor } from './pdf-data-extractor';
import { PdfTagType, ScopeType } from './text-extraction/enumerator';
import { PdfRotationAngle, _getPageIndex, PdfPen, PdfTextBoxField, PdfComboBoxField, PdfListBoxField, PdfButtonField, PdfCheckBoxField, PdfRadioButtonListField, PdfSignatureField, _toRectangle } from '@syncfusion/ej2-pdf';
import { _addFontResources, _getXObjectResources } from './utils';
import { _GraphicState } from './graphic-state';
/**
 * Represents the structure element of the PDF document.
 * ```typescript
 * // Load an existing PDF document
 * let document: PdfDocument = new PdfDocument(data, password);
 * // Initialize a new instance of the `PdfDataExtractor` class
 * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
 * // Access the structure element of the PDF document
 * let rootStructureElement: PdfStructureElement = extractor.getStructureElement();
 * // Save the document
 * document.save('output.pdf');
 * // Destroy the document
 * document.destroy();
 * ```
 */
var PdfStructureElement = /** @class */ (function () {
    function PdfStructureElement() {
        this._text = '';
        this._tagType = PdfTagType.none;
        this._contentId = [];
        this._mcid = -1;
        this._textCollection = [];
        this._title = '';
        this._scope = ScopeType.none;
    }
    Object.defineProperty(PdfStructureElement.prototype, "text", {
        /**
         * Gets the text of the tag (Read only).
         *
         * @returns {string} The extracted text from the structure element.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the extracted text of the structure element.
         * let text: string = element.text;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._text;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "alternateText", {
        /**
         * Gets the alternate text of the tag (Read only).
         *
         * @returns {string} The alternate text from the structure element.
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the alternate text of the structure element.
         * let altText: string = element.alternateText;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._alternateText;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "parent", {
        /**
         * Gets the parent element (Read only).
         *
         * @returns {PdfStructureElement}  The parent PdfStructureElement.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the parent of the structure element.
         * let parentElement: PdfStructureElement = element.parent;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._parent;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "abbreviation", {
        /**
         * Gets the abbreviation (Read only).
         *
         * @returns {string} Abbreviation
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the abbreviation of the structure element.
         * let abbreviation: string = element.abbreviation;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._abbreviation;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "tagType", {
        /**
         * Gets the tag type of the structure element (Read only).
         *
         * @returns {PdfTagType} The tag type of the structure element.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the tag type of the structure element.
         * let type: PdfTagType = element.tagType;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._tagType;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "actualText", {
        /**
         * Gets the actual text of the structure element (Read only).
         *
         * @returns {string} The actual text associated with the structure element.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the actual text of the structure element.
         * let actualText: string = element.actualText;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._actualText;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "page", {
        /**
         * Gets the page associated with the structure element (Read only).
         *
         * @returns {PdfPage} The PdfPage where the structure element is located.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the page of the structure element.
         * let elementPage: PdfPage = element.page;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            if (!(this._page)) {
                this._page = this._getPageFromElement(this);
            }
            var parent = this.parent;
            if (!(this._page) && parent) {
                this._page = this._getPageFromElement(parent);
                if (!(this._page)) {
                    this._page = this._getChildPage();
                }
            }
            return this._page;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "bounds", {
        /**
         * Gets the bounds of the structure element (Read only).
         *
         * @returns {Rectangle} The bounds of the structure element as an array in the format [x, y, width, height].
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the bounds of the structure element.
         * let elementBounds: Rectangle = element.bounds;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._bounds;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "childElements", {
        /**
         * Gets the child elements of this structure element (Read only).
         *
         * @returns {PdfStructureElement[]} An array of child PdfStructureElement objects.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the child elements of the structure element.
         * let childElements: PdfStructureElement[] = element.childElements;
         * // Iterate through each child element
         * childElements.forEach((childElement: PdfStructureElement) => {
         *    // Do something with each child element
         * });
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._childElements;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "language", {
        /**
         * Gets the language of the structure element (Read only).
         *
         * @returns {string} The language code of the structure element.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the language of the structure element.
         * let language: string = element.language;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._language;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "order", {
        /**
         * Gets the order of the structure element (Read only).
         *
         * @returns {number} The order index representing the position of the structure element.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the order of the structure element.
         * let elementOrder: number = element.order;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._order;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "title", {
        /**
         * Gets the title of the structure element (Read only).
         *
         * @returns {string} The title of the structure element.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the title of the structure element.
         * let elementTitle: string = element.title;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._title;
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(PdfStructureElement.prototype, "scope", {
        /**
         * Gets the scope type of the structure element (Read only).
         *
         * @returns {ScopeType} The scope type indicating the context or area covered by the structure element, such as row, column, or both.
         *
         * ```typescript
         * // Load an existing PDF document
         * let document: PdfDocument = new PdfDocument(data, password);
         * // Initialize a new instance of the `PdfDataExtractor` class
         * let extractor: PdfDataExtractor = new PdfDataExtractor(document);
         * // Extract structure elements from a particular page in the PDF document
         * let page: PdfPage = document.getPage(0);
         * let pageElements: PdfStructureElement[] = extractor.getStructureElements(page);
         * let element: PdfStructureElement = pageElements[0];
         * // Gets the scope type of the structure element.
         * let scopeType: ScopeType = element.scope;
         * // Save the document
         * document.save('output.pdf');
         * // Destroy the document
         * document.destroy();
         * ```
         */
        get: function () {
            return this._scope;
        },
        enumerable: true,
        configurable: true
    });
    PdfStructureElement._load = function (document, dictionary, order, parent) {
        var element = new PdfStructureElement();
        element._document = document;
        element._dictionary = dictionary;
        element._parent = parent;
        element._order = order;
        element._childElements = [];
        element._bounds = { x: 0, y: 0, width: 0, height: 0 };
        element._language = '';
        return element;
    };
    PdfStructureElement.prototype._getPageFromElement = function (element) {
        if (element._page) {
            return element._page;
        }
        if (element._pageDictionary) {
            var document_1 = this._document;
            if (!(this._page) && document_1) {
                var pageIndex = _getPageIndex(document_1, element._pageDictionary);
                if (pageIndex !== -1) {
                    this._page = this._document.getPage(pageIndex);
                }
            }
        }
        return this._page;
    };
    PdfStructureElement.prototype._getChildPage = function () {
        var page;
        var elements = this.childElements;
        if (elements.length > 0) {
            for (var i = 0; i < elements.length; i++) {
                var childElement = elements[Number.parseInt(i.toString(), 10)];
                page = this._getPageFromElement(childElement);
                if (page) {
                    break;
                }
                else {
                    page = childElement._getChildPage();
                }
            }
        }
        return page;
    };
    PdfStructureElement.prototype._getTaggedContent = function (elements) {
        var textExtractor = new PdfDataExtractor(this._document);
        textExtractor._extractTaggedText = true;
        var pageTextLine = new Map();
        var _loop_1 = function (i) {
            var element = elements[Number.parseInt(i.toString(), 10)];
            var elementDictionary = element._dictionary;
            var graphicState = new _GraphicState();
            if (element) {
                var page = void 0;
                if (element) {
                    page = element.page;
                }
                var textLineCollection_1;
                if (page) {
                    textLineCollection_1 = pageTextLine.get(page._pageIndex);
                }
                if (!textLineCollection_1 && page) {
                    var fontCollection = new Map();
                    var xObjectCollection = new Map(); //eslint-disable-line
                    if (page._pageDictionary.has('Resources')) {
                        var pageResources = page._pageDictionary.get('Resources');
                        fontCollection = _addFontResources(pageResources, this_1._document._crossReference);
                        xObjectCollection = _getXObjectResources(pageResources, this_1._document._crossReference);
                    }
                    textExtractor._renderText(page, fontCollection, xObjectCollection, graphicState);
                    textLineCollection_1 = textExtractor._textLine.slice();
                    pageTextLine.set(page._pageIndex, textLineCollection_1);
                }
                var textElements_1 = textExtractor._mcidTextMap;
                var resultString_1 = '';
                var tagBounds_1 = { x: Infinity, y: Infinity, width: 0, height: 0 };
                element._contentId.forEach(function (contentId) {
                    if (textElements_1.has(contentId)) {
                        var textArray = textElements_1.get(contentId);
                        textArray.forEach(function (textSegment) {
                            var textLineIndex = textLineCollection_1.findIndex(function (line) {
                                return line.text.trim() === textSegment.trim();
                            });
                            if (textLineIndex !== -1) {
                                var textLine = textLineCollection_1[Number.parseInt(textLineIndex.toString(), 10)];
                                resultString_1 += textSegment;
                                var x = textLine.bounds.x;
                                var y = textLine.bounds.y;
                                var width = textLine.bounds.width;
                                var height = textLine.bounds.height;
                                var _a = [x, y, width, height], lineX = _a[0], lineY = _a[1], lineWidth = _a[2], lineHeight = _a[3];
                                var currentBounds = {
                                    x: lineX,
                                    y: lineY,
                                    width: lineWidth,
                                    height: lineHeight
                                };
                                tagBounds_1.x = Math.min(tagBounds_1.x, currentBounds.x);
                                tagBounds_1.y = Math.min(tagBounds_1.y, currentBounds.y);
                                tagBounds_1.width = Math.max(tagBounds_1.width, currentBounds.x + currentBounds.width - tagBounds_1.x);
                                tagBounds_1.height = Math.max(tagBounds_1.height, currentBounds.y + currentBounds.height - tagBounds_1.y);
                                textLineCollection_1.splice(textLineIndex, 1);
                            }
                        });
                    }
                });
                if (resultString_1 && element._tagType && element._tagType !== PdfTagType.figure) {
                    element._text = resultString_1;
                    element._bounds = { x: tagBounds_1.x, y: tagBounds_1.y, width: tagBounds_1.width, height: tagBounds_1.height };
                    page.graphics.drawRectangle(element._bounds, new PdfPen({ r: 255, g: 0, b: 0 }, 1));
                }
                if (!element._actualText && elementDictionary && elementDictionary.has('ActualText')) {
                    var actualText = elementDictionary.get('ActualText');
                    if (actualText) {
                        element._actualText = actualText;
                    }
                }
                if (!element._alternateText && elementDictionary && elementDictionary.has('Alt')) {
                    var altText = elementDictionary.get('Alt');
                    if (altText) {
                        element._alternateText = altText;
                    }
                }
                if (!element._language && elementDictionary && elementDictionary.has('Lang')) {
                    var language = elementDictionary.get('Lang');
                    if (language) {
                        element._language = language;
                    }
                }
                if (!element._title && elementDictionary && elementDictionary.has('T')) {
                    var title = elementDictionary.get('T');
                    if (title && typeof title === 'string') {
                        element._title = title.trim();
                    }
                }
                if (element.parent && (element.parent.tagType === PdfTagType.annotation ||
                    element.parent.tagType === PdfTagType.link || element.parent.tagType === PdfTagType.form)) {
                    if (elementDictionary && elementDictionary.has('Obj')) {
                        var obj = (elementDictionary.has('Obj') ? 'Obj' : 'obj');
                        var dict = elementDictionary.get(obj);
                        if (element.parent.tagType === PdfTagType.annotation || element.parent.tagType === PdfTagType.link) {
                            if (dict && element.page && element.page.annotations.count > 0) {
                                for (var i_1 = 0; i_1 < element.page.annotations.count; i_1++) {
                                    var annot = element.page.annotations.at(Number.parseInt(i_1.toString(), 10));
                                    if (annot._dictionary === dict) {
                                        element._bounds = this_1._calculateBounds(element, annot.bounds);
                                    }
                                }
                            }
                        }
                        else {
                            if (dict && this_1._document.form.count > 0) {
                                for (var i_2 = 0; i_2 < this_1._document.form.count; i_2++) {
                                    var field = this_1._document.form.fieldAt(Number.parseInt(i_2.toString(), 10));
                                    if (field && field._dictionary === dict) {
                                        element._bounds = { x: field.bounds.x, y: field.bounds.y, width: field.bounds.width,
                                            height: field.bounds.height };
                                    }
                                    else if (field && field._kids && field._kids.length > 0) {
                                        if (field instanceof PdfTextBoxField) {
                                            var textBox = field;
                                            for (var j = 0; j < textBox.itemsCount; j++) {
                                                var textBoxitem = textBox.itemAt(Number.parseInt(j.toString(), 10));
                                                if (textBoxitem && textBoxitem._dictionary === dict) {
                                                    var textBoxBounds = textBox.itemAt(Number.parseInt(j.toString(), 10)).bounds;
                                                    element._bounds = { x: textBoxBounds.x, y: textBoxBounds.y,
                                                        width: textBoxBounds.width, height: textBoxBounds.height };
                                                }
                                            }
                                        }
                                        else if (field instanceof PdfComboBoxField) {
                                            var comboBox = field;
                                            if (comboBox && comboBox.itemsCount > 0) {
                                                for (var j = 0; j < comboBox.itemsCount; j++) {
                                                    var comboBoxitem = comboBox.itemAt(Number.parseInt(j.toString(), 10));
                                                    if (comboBoxitem && comboBoxitem._dictionary === dict) {
                                                        var comboBoxBounds = comboBox.itemAt(Number.parseInt(j.toString(), 10)).bounds;
                                                        element._bounds = { x: comboBoxBounds.x, y: comboBoxBounds.y,
                                                            width: comboBoxBounds.width, height: comboBoxBounds.height };
                                                    }
                                                }
                                            }
                                        }
                                        else if (field instanceof PdfListBoxField) {
                                            var listBox = field;
                                            if (listBox && listBox.itemsCount > 0) {
                                                for (var j = 0; j < listBox.itemsCount; j++) {
                                                    var listBoxItem = listBox.itemAt(Number.parseInt(j.toString(), 10));
                                                    if (listBoxItem && listBoxItem._dictionary === dict) {
                                                        var listBoxBounds = listBox.itemAt(Number.parseInt(j.toString(), 10)).bounds;
                                                        element._bounds = { x: listBoxBounds.x, y: listBoxBounds.y,
                                                            width: listBoxBounds.width, height: listBoxBounds.height };
                                                    }
                                                }
                                            }
                                        }
                                        else if (field instanceof PdfButtonField) {
                                            var buttonField = field;
                                            for (var j = 0; j < buttonField.itemsCount; j++) {
                                                var buttonFieldItem = buttonField.itemAt(Number.parseInt(j.toString(), 10));
                                                if (buttonFieldItem && buttonFieldItem._dictionary === dict) {
                                                    var buttonBounds = buttonField.itemAt(Number.parseInt(j.toString(), 10)).bounds;
                                                    element._bounds = { x: buttonBounds.x, y: buttonBounds.y,
                                                        width: buttonBounds.width, height: buttonBounds.height };
                                                }
                                            }
                                        }
                                        else if (field instanceof PdfCheckBoxField) {
                                            var checkField = field;
                                            for (var j = 0; j < checkField.itemsCount; j++) {
                                                var checkFieldItem = checkField.itemAt(Number.parseInt(j.toString(), 10));
                                                if (checkFieldItem && checkFieldItem._dictionary === dict) {
                                                    var checkBounds = checkField.itemAt(Number.parseInt(j.toString(), 10)).bounds;
                                                    element._bounds = { x: checkBounds.x, y: checkBounds.y,
                                                        width: checkBounds.width, height: checkBounds.height };
                                                }
                                            }
                                        }
                                        else if (field instanceof PdfRadioButtonListField) {
                                            var radioField = field;
                                            for (var j = 0; j < radioField.itemsCount; j++) {
                                                var radioFieldItem = radioField.itemAt(Number.parseInt(j.toString(), 10));
                                                if (radioFieldItem && radioFieldItem._dictionary === dict) {
                                                    var radioBounds = radioField.itemAt(Number.parseInt(j.toString(), 10)).bounds;
                                                    element._bounds = { x: radioBounds.x, y: radioBounds.y, width: radioBounds.width,
                                                        height: radioBounds.height };
                                                }
                                            }
                                        }
                                        else if (field instanceof PdfSignatureField) {
                                            var signField = field;
                                            for (var j = 0; j < signField.itemsCount; j++) {
                                                var signFieldItem = signField.itemAt(Number.parseInt(j.toString(), 10));
                                                if (signFieldItem && signFieldItem._dictionary === dict) {
                                                    var signBounds = signField.itemAt(Number.parseInt(j.toString(), 10)).bounds;
                                                    element._bounds = { x: signBounds.x, y: signBounds.y, width: signBounds.width,
                                                        height: signBounds.height };
                                                }
                                            }
                                        }
                                    }
                                    else if (dict.has('Parent')) {
                                        var parentDictionary = dict.get('Parent');
                                        if (parentDictionary && field._dictionary === parentDictionary) {
                                            element._bounds = { x: field.bounds.x, y: field.bounds.y, width: field.bounds.width,
                                                height: field.bounds.height };
                                        }
                                    }
                                    element._bounds = this_1._calculateBounds(element, element._bounds);
                                }
                            }
                        }
                    }
                }
                else if (element._tagType === PdfTagType.figure) {
                    var assignedBounds = false;
                    var bounds = textExtractor._getFigureBounds(element, element.page);
                    if (bounds) {
                        element._bounds = bounds;
                        assignedBounds = true;
                    }
                    if (!assignedBounds) {
                        element._bounds = { x: 0, y: 0, width: 0, height: 0 };
                    }
                    if (elementDictionary && elementDictionary.has('A')) {
                        var dictionary = elementDictionary.get('A');
                        if (dictionary && dictionary.has('BBox')) {
                            var bbox = dictionary.get('BBox');
                            if (bbox && bbox.length > 0 && element._bounds) {
                                var bboxArray = _toRectangle(bbox);
                                if (((element.page && element._page.rotation === PdfRotationAngle.angle0 ||
                                    element.page.rotation === PdfRotationAngle.angle180))
                                    || ((element.page.rotation === PdfRotationAngle.angle90 ||
                                        element.page.rotation === PdfRotationAngle.angle270))) {
                                    if (element.page.rotation === PdfRotationAngle.angle0) {
                                        element._bounds = { x: element._bounds.x, y: element._bounds.y - bboxArray.height,
                                            width: bboxArray.width, height: bboxArray.height };
                                    }
                                    else if (element.page.rotation === PdfRotationAngle.angle90) {
                                        element._bounds = { x: element._bounds.x, y: element._bounds.y,
                                            width: bboxArray.height, height: bboxArray.width };
                                    }
                                    else if (element.page.rotation === PdfRotationAngle.angle180) {
                                        element._bounds = { x: element._bounds.x - bboxArray.width, y: element._bounds.y,
                                            width: bboxArray.width, height: bboxArray.height };
                                    }
                                    else if (element.page.rotation === PdfRotationAngle.angle270) {
                                        element._bounds = { x: element._bounds.x - bboxArray.height, y: element._bounds.y -
                                                bboxArray.width, width: bboxArray.height, height: bboxArray.width };
                                    }
                                }
                            }
                        }
                    }
                }
            }
        };
        var this_1 = this;
        for (var i = 0; i < elements.length; i++) {
            _loop_1(i);
        }
    };
    PdfStructureElement.prototype._calculateBounds = function (element, bounds) {
        if (element && element._page) {
            var rotation = element._page.rotation;
            if (rotation !== PdfRotationAngle.angle0) {
                if (rotation === PdfRotationAngle.angle90) {
                    bounds = { x: element.page.size.height - bounds.y - bounds.height, y: bounds.x,
                        width: bounds.height, height: bounds.width };
                }
                else if (rotation === PdfRotationAngle.angle180) {
                    bounds = { x: element.page.size.width - bounds.x - bounds.width,
                        y: element.page.size.height - bounds.y - bounds.width, width: bounds.width, height: bounds.height };
                }
                else if (rotation === PdfRotationAngle.angle270) {
                    bounds = { x: bounds.y, y: element.page.size.width - bounds.x - bounds.width,
                        width: bounds.height, height: bounds.width };
                }
            }
        }
        return bounds;
    };
    PdfStructureElement.prototype._getTagType = function (value) {
        var type = PdfTagType.none;
        switch (value) {
            case 'P':
                type = PdfTagType.paragraph;
                break;
            case 'Figure':
                type = PdfTagType.figure;
                break;
            case 'Art':
                type = PdfTagType.article;
                break;
            case 'Annot':
                type = PdfTagType.annotation;
                break;
            case 'Bibentry':
                type = PdfTagType.bibliographyEntry;
                break;
            case 'BlockQuote':
                type = PdfTagType.blockQuotation;
                break;
            case 'Caption':
                type = PdfTagType.caption;
                break;
            case 'Code':
                type = PdfTagType.code;
                break;
            case 'Div':
                type = PdfTagType.division;
                break;
            case 'Document':
                type = PdfTagType.documentType;
                break;
            case 'Form':
                type = PdfTagType.form;
                break;
            case 'Formula':
                type = PdfTagType.formula;
                break;
            case 'Index':
                type = PdfTagType.index;
                break;
            case 'H':
            case 'Title':
                type = PdfTagType.heading;
                break;
            case 'H1':
                type = PdfTagType.headingLevel1;
                break;
            case 'H2':
                type = PdfTagType.headingLevel2;
                break;
            case 'H3':
                type = PdfTagType.headingLevel3;
                break;
            case 'H4':
                type = PdfTagType.headingLevel4;
                break;
            case 'H5':
                type = PdfTagType.headingLevel5;
                break;
            case 'H6':
                type = PdfTagType.headingLevel6;
                break;
            case 'Lbl':
                type = PdfTagType.label;
                break;
            case 'Link':
                type = PdfTagType.link;
                break;
            case 'L':
                type = PdfTagType.list;
                break;
            case 'LI':
                type = PdfTagType.listItem;
                break;
            case 'LBody':
                type = PdfTagType.listBody;
                break;
            case 'Note':
                type = PdfTagType.note;
                break;
            case 'Part':
                type = PdfTagType.part;
                break;
            case 'Quote':
                type = PdfTagType.quotation;
                break;
            case 'Reference':
                type = PdfTagType.reference;
                break;
            case 'Sect':
                type = PdfTagType.section;
                break;
            case 'Span':
            case 'ParagraphSpan':
            case 'HyphenSpan':
            case 'StyleSpan':
                type = PdfTagType.span;
                break;
            case 'Table':
                type = PdfTagType.table;
                break;
            case 'TD':
                type = PdfTagType.tableDataCell;
                break;
            case 'TH':
                type = PdfTagType.tableHeader;
                break;
            case 'TOC':
                type = PdfTagType.tableOfContent;
                break;
            case 'TOCI':
                type = PdfTagType.tableOfContentItem;
                break;
            case 'TR':
                type = PdfTagType.tableRow;
                break;
            case 'THead':
                type = PdfTagType.tableHeaderRowGroup;
                break;
            case 'TBody':
                type = PdfTagType.tableBodyRowGroup;
                break;
            case 'TFoot':
                type = PdfTagType.tableFooterRowGroup;
                break;
        }
        return type;
    };
    PdfStructureElement.prototype._parseContent = function (element) {
        var abbreviation;
        if (element && element.length > 1) {
            var result = element[1].replace('/', '');
            var regexPattern = '[ \\(\\)><]';
            var regex = new RegExp(regexPattern, 'g');
            var layerEntries = result.replace('/', '').split(regex);
            for (var i = 0; i < layerEntries.length; i++) {
                var entry = layerEntries[Number.parseInt(i.toString(), 10)];
                if (entry === 'E') {
                    var expansionParts = [];
                    var j = i + 1;
                    while (j < layerEntries.length && layerEntries[j] !== 'MCID') {
                        var entry_1 = layerEntries[j];
                        if (typeof entry_1 === 'string' && entry_1.trim().length > 0) {
                            expansionParts.push(entry_1);
                        }
                        j++;
                    }
                    abbreviation = expansionParts.join(' ');
                    i = j - 1;
                }
                else if (entry === 'MCID') {
                    this._mcid = parseInt(layerEntries[Number.parseInt(i.toString(), 10) + 1], 10);
                    if (abbreviation) {
                        if (!this._abbreviationCollection) {
                            this._abbreviationCollection = new Map();
                        }
                        this._abbreviationCollection.set(this._mcid, abbreviation);
                    }
                }
            }
        }
        return this._mcid;
    };
    return PdfStructureElement;
}());
export { PdfStructureElement };
