import { TypeEditor } from "../../TypeEditor.js";
import ObservedBoolean from "../../../../../src/core/model/ObservedBoolean.js";
import { CheckboxView } from "../../../../../src/view/elements/CheckboxView.js";

export class BooleanEditor extends TypeEditor {
    build(parent, field,registry) {
        const v = new ObservedBoolean(field.adapter.read(parent, field.name));

        const ctrl = new CheckboxView({
            value: v
        });

        function sync_up() {
            v.set(field.adapter.read(parent, field.name));
        }

        function sync_down() {
            field.adapter.write(parent, field.name, v.getValue());
        }

        ctrl.on.linked.add(sync_up);
        v.onChanged.add(sync_down);


        return ctrl;
    }
}
