import { TypeEditor } from "../TypeEditor.js";
import DropDownSelectionView from "../../../../src/view/elements/DropDownSelectionView.js";
import List from "../../../../src/core/collection/list/List.js";
import { objectKeyByValue } from "../../../../src/core/model/object/objectKeyByValue.js";

export class ObservedEnumEditor extends TypeEditor {
    build(parent, field, registry) {

        /**
         * @type {ObservedEnum}
         */
        const value = field.adapter.read(parent, field.name);

        const valueSet = value.getValidValueSet();

        const ctrl = new DropDownSelectionView(
            new List(Object.keys(valueSet)), {
                changeListener(key) {
                    const v = valueSet[key];

                    value.set(v);
                }
            }
        );

        ctrl.on.linked.add(() => {
            ctrl.setSelectedValue(objectKeyByValue(valueSet, value.getValue()));
        })

        return ctrl;
    }
}
