// /AIWK_COMMON/core/aiwk_event.js
// AIWK EXT4 v003
(function () {
    "use strict";

    const AIWK = window.AIWK;
    const map = {};

    AIWK.events = {
        on: function (name, fn) {
            if (!map[name]) {
                map[name] = [];
            }
            map[name].push(fn);
            return function () {
                AIWK.events.off(name, fn);
            };
        },
        off: function (name, fn) {
            if (!map[name]) {
                return;
            }
            map[name] = map[name].filter(function (item) {
                return item !== fn;
            });
        },
        emit: function (name, payload) {
            (map[name] || []).slice().forEach(function (fn) {
                try {
                    fn(payload);
                } catch (err) {
                    console.error("AIWK event error", name, err);
                }
            });
        }
    };
}());
