/**
 * DevExtreme (esm/integration/knockout/event_registrator.js)
 * Version: 24.1.7-build-24272-1937
 * Build date: Sat Sep 28 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import $ from "../../core/renderer";
import eventsEngine from "../../events/core/events_engine";
import ko from "knockout";
import {
    isPlainObject
} from "../../core/utils/type";
import eventRegistratorCallbacks from "../../events/core/event_registrator_callbacks";
import {
    addNamespace
} from "../../events/utils/index";
if (ko) {
    eventRegistratorCallbacks.add((function(name) {
        const koBindingEventName = addNamespace(name, name + "Binding");
        ko.bindingHandlers[name] = {
            update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
                const $element = $(element);
                const unwrappedValue = ko.utils.unwrapObservable(valueAccessor());
                const eventSource = unwrappedValue.execute ? unwrappedValue.execute : unwrappedValue;
                eventsEngine.off($element, koBindingEventName);
                eventsEngine.on($element, koBindingEventName, isPlainObject(unwrappedValue) ? unwrappedValue : {}, (function(e) {
                    eventSource.call(viewModel, viewModel, e)
                }))
            }
        }
    }))
}
