"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Field_1 = require("../../../../../Class/Field");
const Graph_1 = require("../../../../../Class/Graph");
const _ids_1 = require("../../../../_ids");
const Option_1 = __importDefault(require("../Option"));
const findFirstOption = (unit) => {
    if (unit instanceof Option_1.default) {
        return unit;
    }
    else if (unit instanceof Graph_1.Graph) {
        const units = unit.getUnits();
        for (const unitId in units) {
            const unit = units[unitId];
            const option = findFirstOption(unit);
            if (option) {
                return option;
            }
        }
        return null;
    }
    else {
        return null;
    }
};
class Select extends Field_1.Field {
    constructor(system) {
        super({
            i: ['value', 'style', 'attr'],
            o: ['value'],
        }, {}, system, _ids_1.ID_SELECT, 'value');
        this._first_option = null;
        this._defaultState = {};
        const setFirstOption = (option) => { };
        const appendChildListener = (_, child) => {
            if (!this._first_option) {
                const option = findFirstOption(child);
                if (option) {
                    this._first_option = option;
                    const value = option.peakInput('value');
                    if (value !== undefined) {
                        void this.set('value', value);
                        this._forward('value', value);
                        this._backward('value');
                    }
                }
            }
        };
        const removeChildListener = () => {
            if (this._first_option) {
                const value = this._first_option.peakInput('value');
                const value_was_first_option = this._i.value === value;
                this._first_option = null;
                for (const child of this._children) {
                    const option = findFirstOption(child);
                    if (option) {
                        this._first_option = option;
                        const value = option.peakInput('value');
                        if (value !== undefined) {
                            void this.set('value', value);
                            this._forward('value', value);
                            if (value_was_first_option) {
                                this._backward('value');
                            }
                        }
                        break;
                    }
                }
                if (!this._first_option) {
                    void this.set('value', undefined);
                    this._forwarding_empty = true;
                    this._forward_empty('value');
                    this._forwarding_empty = false;
                }
            }
        };
        this.addListener('append_child', appendChildListener);
        this.addListener('remove_child', removeChildListener);
    }
    onDataInputDrop(name) {
        if (this._first_option) {
            const value = this._first_option.peakInput('value');
            if (value !== undefined) {
                void this.set('value', value);
                this._forward('value', value);
            }
        }
    }
}
exports.default = Select;
//# sourceMappingURL=index.js.map