"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const isEventTarget_1 = __importDefault(require("./isEventTarget")); const eventOptionsSupported_1 = __importDefault(require("./eventOptionsSupported")); const on_1 = __importDefault(require("./on")); const off_1 = __importDefault(require("./off")); const manuelOnce = (elm, handler, when) => { const offHandler = (e) => { if (when && when(e) !== true) { return true; } off_1.default(elm, e.type, offHandler); return 'handleEvent' in handler ? handler.handleEvent(e) : handler(e); }; return offHandler; }; const bind = (elm, eventNames, handler, options) => { const noOptions = !eventOptionsSupported_1.default(); const _a = options || {}, { when } = _a, eventOptions = __rest(_a, ["when"]); eventOptions.once = !when; const eventHandler = when || noOptions ? manuelOnce(elm, handler, when) : handler; on_1.default(elm, eventNames, eventHandler, eventOptions); return () => off_1.default(elm, eventNames, eventHandler, eventOptions); }; function once(elm, eventNames, handler, options) { if (!isEventTarget_1.default(elm)) { options = handler; handler = eventNames; eventNames = elm; elm = document; } return bind(elm, eventNames, handler, options); } exports.default = once;