"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint @typescript-eslint/no-inferrable-types: "off", no-var: "off", prefer-const: "off" */
/**
 * Liza Field Context Store
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework
 *
 *  Liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.FieldContextStore = void 0;
/**
 * Context responsible for storing the original
 * content and sibling. Clones the content
 * and resets the index attributes before returning
 */
var FieldContextStore = /** @class */ (function () {
    /**
     * Initialize FieldContextStore
     *
     * @param _element_id  field identifier
     * @param _content     field content
     * @param _sibling     sibling field content
     * @param _is_internal if user is internal
     */
    function FieldContextStore(_element_id, _content, _sibling, _is_internal) {
        if (_sibling === void 0) { _sibling = null; }
        if (_is_internal === void 0) { _is_internal = false; }
        this._element_id = _element_id;
        this._content = _content;
        this._sibling = _sibling;
        this._is_internal = _is_internal;
        /**
         * Original content position relative to its parent
         */
        this._position = 0;
        /**
         * Whether field is a subfield
         */
        this._is_subfield = false;
        /**
         * Whether subfield determination has happened
         */
        this._subfield_determined = false;
        /**
         * The name of the parent field for a subfield
         */
        this._subfield_parent_name = '';
        /**
         * Field values by index
         */
        this._field_values = [];
        /**
         * Field options by index
         */
        this._field_options = [];
        this._setContentPosition();
    }
    Object.defineProperty(FieldContextStore.prototype, "content", {
        /**
         * Return content
         *
         * @returns content
         */
        get: function () {
            return this._displayInternalFields(this._content);
        },
        enumerable: false,
        configurable: true
    });
    Object.defineProperty(FieldContextStore.prototype, "siblingContent", {
        /**
         * Return sibling content
         *
         * @returns sibling content
         */
        get: function () {
            if (this._sibling !== null) {
                return this._displayInternalFields(this._sibling);
            }
            return null;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Return content clone
     *
     * @param index - index of content
     *
     * @returns clone
     */
    FieldContextStore.prototype.getContentClone = function (index) {
        var content_clone = this._content.cloneNode(true);
        var content = this._setElementIdIndexes(content_clone, index);
        return this._displayInternalFields(content);
    };
    /**
     * Return sibling content clone
     *
     * @param index - index of content
     *
     * @returns sibling clone
     */
    FieldContextStore.prototype.getSiblingContentClone = function (index) {
        if (this._sibling === null) {
            return null;
        }
        var sibling_clone = this._sibling.cloneNode(true);
        var content = this._setElementIdIndexes(sibling_clone, index);
        return this._displayInternalFields(content);
    };
    /**
     * Detaches content and sibling from its parent
     *
     * The content will stay in memory so it can be cloned
     */
    FieldContextStore.prototype.detach = function () {
        if (this._content.parentElement) {
            this._content.parentElement.removeChild(this._content);
            if (this._sibling !== null && this._sibling.parentElement) {
                this._sibling.parentElement.removeChild(this._sibling);
            }
        }
    };
    Object.defineProperty(FieldContextStore.prototype, "position", {
        /**
         * Return position index
         *
         * @return position index
         */
        get: function () {
            return this._position;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Set value of the field by index
     * This is used to store the value
     * when a FieldContext does not exist
     * for this index
     *
     * @param index - index of field
     * @param value - value to set
     */
    FieldContextStore.prototype.setValueByIndex = function (index, value) {
        this._field_values[index] = value;
    };
    /**
     * Return value of the field by index
     *
     * Once returned, delete array key
     * to avoid unnecessary calls to DOM
     *
     * @param index - index of field
     */
    FieldContextStore.prototype.getValueByIndex = function (index) {
        var value = this._field_values[index];
        if (value !== undefined) {
            delete this._field_values[index];
            return value;
        }
        return '';
    };
    /**
     * Return true if field value was stored for index
     *
     * @param index - index of field
     */
    FieldContextStore.prototype.hasValueByIndex = function (index) {
        return this._field_values[index] !== undefined;
    };
    /**
     * Set options of the field by index
     * This is used to store the options
     * when a FieldContext does not exist
     * for this index
     *
     * @param index - index of field
     * @param options - list of options to set
     */
    FieldContextStore.prototype.setOptionsByIndex = function (index, options) {
        this._field_options[index] = options;
    };
    /**
     * Return options of the field by index
     *
     * Once returned, delete array key
     * to avoid unnecessary calls to DOM
     *
     * @param index - index of field
     */
    FieldContextStore.prototype.getOptionsByIndex = function (index) {
        var options = this._field_options[index];
        if (options !== undefined) {
            delete this._field_options[index];
            return options;
        }
        return [];
    };
    /**
     * Return true if field options were stored for index
     *
     * @param index - index of field
     */
    FieldContextStore.prototype.hasOptionsByIndex = function (index) {
        return this._field_options[index] !== undefined;
    };
    /**
     * Sets the position of the content
     * in relation to the parent node.
     */
    FieldContextStore.prototype._setContentPosition = function () {
        var content = this._sibling;
        if (content === null) {
            content = this._content;
        }
        var parent = content.parentNode;
        this._position = (Array.prototype.indexOf.call(parent === null || parent === void 0 ? void 0 : parent.children, content));
    };
    /**
     * Return whether the field in the content represents a sub-field
     */
    FieldContextStore.prototype.isSubField = function () {
        if (this._subfield_determined === true) {
            return this._is_subfield;
        }
        return this._isSubFieldIndicator();
    };
    Object.defineProperty(FieldContextStore.prototype, "subFieldParentName", {
        /**
         * Return the sub-field's parent name
         *
         * This is used by GroupContext to find
         * the parent FieldContext of a sub-field
         *
         * @return sub-field's parent name
         */
        get: function () {
            return this._subfield_parent_name;
        },
        enumerable: false,
        configurable: true
    });
    /**
     * Determine whether the field in the content represents a sub-field
     *
     * A sub-field is a field within a field; the distinction is important
     * because we don't want operations on a sub-field affecting
     * its parent.
     */
    FieldContextStore.prototype._isSubFieldIndicator = function () {
        var field_element = this._content.querySelector('#' + this._element_id);
        this._subfield_determined = true;
        if (field_element === null) {
            return this._is_subfield;
        }
        var parent = field_element.parentElement;
        // A subfield's parent has a 'widget' class value
        this._is_subfield = !!(parent && parent.classList.contains('widget'));
        if (this._is_subfield === true && parent !== null) {
            // capture the subfield's parent field name
            this._subfield_parent_name = parent.getAttribute('data-field-name') || '';
        }
        return this._is_subfield;
    };
    /**
     * If the user is internal, display internal fields
     *
     * @param content - the content
     *
     * @returns modified content
     */
    FieldContextStore.prototype._displayInternalFields = function (content) {
        if (this._is_internal === true) {
            if (content.classList.contains('hidden') &&
                content.classList.contains('i')) {
                content.classList.remove('hidden');
            }
            // Iterate over child elements
            var elements = content.querySelectorAll('.hidden.i');
            for (var i = 0; i < elements.length; i++) {
                var element = elements[i];
                element.classList.remove('hidden');
            }
        }
        return content;
    };
    /**
     * Sets the index (for the name attribute) of all given elements
     *
     * The name format is expected to be: name_i, where i is the index.
     *
     * @param content - the content
     *
     * @returns modified content
     */
    FieldContextStore.prototype._setElementIdIndexes = function (content, index) {
        var elements = [].slice.call(content.getElementsByTagName('*'));
        // Add the content element to the array
        elements.push(content);
        for (var i = 0; i < elements.length; i++) {
            var element = elements[i];
            var id = element.getAttribute('id') || '';
            var element_data = null;
            // grab the index from the id if found
            if ((element_data = id.match(/^(.*?)(\d+)$/))) {
                // regenerate the id
                element.setAttribute('id', element_data[1] + index);
            }
            element.setAttribute('data-index', index.toString());
        }
        return content;
    };
    return FieldContextStore;
}());
exports.FieldContextStore = FieldContextStore;
//# sourceMappingURL=data:application/json;base64, 