"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint @typescript-eslint/no-inferrable-types: "off", prefer-const: "off" */
/**
 * Liza Field Context
 *
 *  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.FieldContext = void 0;
/**
 * Context responsible for a specific field in the DOM
 */
var FieldContext = /** @class */ (function () {
    /**
     * Initialize FieldContext
     *
     * @param document to create Document elements
     * @param styler field styler
     * @param element_id field identifier
     * @param content field content
     * @param sibling sibling field content
     */
    function FieldContext(document, styler, element_id, content, sibling) {
        if (sibling === void 0) { sibling = null; }
        this.document = document;
        this.styler = styler;
        this.element_id = element_id;
        this.content = content;
        this.sibling = sibling;
        /**
         * Is attached on the DOM
         */
        this.is_attached = false;
        /**
         * Is field visible to the user
         */
        this.is_visible = false;
        this.setFieldAttached();
    }
    /**
     * Return the element identifier as used on the DOM
     *
     * @returns identifier
     */
    FieldContext.prototype.getElementId = function () {
        return this.element_id;
    };
    /**
     * If the field is attached to the DOM
     */
    FieldContext.prototype.isAttached = function () {
        return this.is_attached;
    };
    /**
     * If the field is visible
     */
    FieldContext.prototype.isVisible = function () {
        return this.is_attached && this.is_visible;
    };
    /**
     * If the content has a parent element,
     * we can safely assume the content
     * is attached to the DOM
     */
    FieldContext.prototype.setFieldAttached = function () {
        if (this.content.parentElement !== null) {
            this.is_attached = true;
            this.is_visible = true;
        }
    };
    /**
     * Set value of the field
     *
     * @param value - value to set
     */
    FieldContext.prototype.setValue = function (value) {
        this.styler.setValue(this.content, value);
    };
    /**
     * Set Options on Select elements
     *
     * @param options - list of options to set
     * @param value - value to set once options exist
     */
    FieldContext.prototype.setOptions = function (options, value) {
        var _a;
        var field_element = (this.content.querySelector('select#' + this.element_id));
        if (field_element === null) {
            return;
        }
        // if new value is not provided reset the old value
        var value_to_set = value !== null && value !== void 0 ? value : field_element === null || field_element === void 0 ? void 0 : field_element.value;
        field_element.innerHTML = '';
        for (var item in options) {
            var opt_value = (_a = options[item]) === null || _a === void 0 ? void 0 : _a.value;
            opt_value = '' + (opt_value !== null && opt_value !== void 0 ? opt_value : '');
            var opt = this.document.createElement('option');
            opt.value = opt_value;
            opt.text = options[item].label;
            field_element.appendChild(opt);
            // if we found our value, select it
            if ('' + opt_value === '' + value_to_set) {
                field_element.value = value_to_set;
                this.setValue(value_to_set);
            }
        }
    };
    /**
     * Show the field
     *
     * @param to - Parent to attach to
     * @param next_element - Next element to attach before
     */
    FieldContext.prototype.show = function (to, next_element) {
        this.attach(to, next_element);
    };
    /**
     * Show the field
     *
     * @param to - Parent to attach to
     * @param next_element - Next element to attach before
     */
    FieldContext.prototype.attach = function (to, next_element) {
        to.insertBefore(this.content, next_element);
        if (this.sibling !== null) {
            to.insertBefore(this.sibling, this.content);
        }
        this.is_attached = true;
        this.is_visible = true;
    };
    /**
     * Hide the field
     */
    FieldContext.prototype.hide = function () {
        this.detach();
    };
    /**
     * Detach the field from the DOM
     */
    FieldContext.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);
            }
        }
        this.is_attached = false;
        this.is_visible = false;
    };
    /**
     * Get the field content or sibling if it exists
     *
     * @returns first in set
     */
    FieldContext.prototype.getFirstOfContentSet = function () {
        return this.sibling || this.content;
    };
    /**
     * Get the field content
     *
     * @returns content
     */
    FieldContext.prototype.getContent = function () {
        return this.content;
    };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