import { a as e, n as t } from './chunk-BneVvdWh.js';
import { S as n } from './iframe-B-AKBcwT.js';
import { t as r } from './jsx-runtime-Bn1Ys6_W.js';
import { t as i } from './compiler-runtime-C9X80vSn.js';
function a({ eventType: e, handler: t, ignoreUsedKeyboardEvents: n = !0, priority: r }) {
    if (!t) return f;
    let i = Math.min(u + d, Math.max(0, (r ?? 0) + d)),
        a = c[e];
    return (
        (a[i] ??= new Set()),
        a[i].add(t),
        c.config.set(t, { ignoreUsedKeyboardEvents: n, priority: i }),
        () => {
            if ((c.config.delete(t), a[i] != null && (a[i].delete(t), !a[i].size))) {
                delete a[i];
                for (let e = i; e > -1 && a[e] == null && a.length === e + 1; e--)
                    a.length = e;
            }
        }
    );
}
function o(e) {
    if (!(!e?.defaultView || e.defaultView.__useKeyboardEventsAttached__))
        return (
            (e.defaultView.__useKeyboardEventsAttached__ = !0),
            e.addEventListener(`keydown`, s),
            e.addEventListener(`keypress`, s),
            e.addEventListener(`keyup`, s),
            () => {
                (e.defaultView && (e.defaultView.__useKeyboardEventsAttached__ = !1),
                    e.removeEventListener(`keydown`, s),
                    e.removeEventListener(`keypress`, s),
                    e.removeEventListener(`keyup`, s));
            }
        );
}
function s(e) {
    let t = c[e.type],
        n = t.length,
        r = g(e);
    for (; n--; ) {
        let i = t[n];
        if (i != null)
            for (let t of i) {
                let n = c.config.get(t);
                if ((!r || n?.ignoreUsedKeyboardEvents === !1) && t(e) === !1) return;
            }
    }
}
var c,
    l,
    u,
    d,
    f,
    p,
    m,
    h,
    g,
    _ = t(() => {
        ((c = { config: new Map(), keydown: [], keypress: [], keyup: [] }),
            (l = -50),
            (u = 50),
            (d = l * -1),
            (f = () => {}),
            (p = new Set([
                `button`,
                `checkbox`,
                `color`,
                `file`,
                `hidden`,
                `image`,
                `radio`,
                `range`,
                `reset`,
                `submit`,
            ])),
            (m = new Set([
                ` `,
                `ArrowDown`,
                `ArrowLeft`,
                `ArrowRight`,
                `ArrowUp`,
                `Enter`,
            ])),
            (h = (e) =>
                e.isContentEditable || e.tagName === `TEXTAREA` || e.tagName === `INPUT`),
            (g = (e) => {
                let t = e.target;
                return t == null || !h(t)
                    ? !1
                    : t.tagName !== `INPUT` || !p.has(t.type)
                      ? !0
                      : m.has(e.key);
            }));
    });
function v(e) {
    let t = (0, S.c)(16),
        {
            ignoreUsedKeyboardEvents: n,
            onKeyDown: r,
            onKeyPress: i,
            onKeyUp: o,
            priority: s,
        } = e,
        c;
    (t[0] === Symbol.for(`react.memo_cache_sentinel`)
        ? ((c = []), (t[0] = c))
        : (c = t[0]),
        (0, C.useEffect)(y, c));
    let l, u;
    (t[1] !== n || t[2] !== r || t[3] !== s
        ? ((l = () =>
              a({
                  eventType: `keydown`,
                  handler: r,
                  ignoreUsedKeyboardEvents: n,
                  priority: s,
              })),
          (u = [n, r, s]),
          (t[1] = n),
          (t[2] = r),
          (t[3] = s),
          (t[4] = l),
          (t[5] = u))
        : ((l = t[4]), (u = t[5])),
        (0, C.useEffect)(l, u));
    let d, f;
    (t[6] !== n || t[7] !== i || t[8] !== s
        ? ((d = () =>
              a({
                  eventType: `keypress`,
                  handler: i,
                  ignoreUsedKeyboardEvents: n,
                  priority: s,
              })),
          (f = [n, i, s]),
          (t[6] = n),
          (t[7] = i),
          (t[8] = s),
          (t[9] = d),
          (t[10] = f))
        : ((d = t[9]), (f = t[10])),
        (0, C.useEffect)(d, f));
    let p, m;
    (t[11] !== n || t[12] !== o || t[13] !== s
        ? ((p = () =>
              a({
                  eventType: `keyup`,
                  handler: o,
                  ignoreUsedKeyboardEvents: n,
                  priority: s,
              })),
          (m = [n, o, s]),
          (t[11] = n),
          (t[12] = o),
          (t[13] = s),
          (t[14] = p),
          (t[15] = m))
        : ((p = t[14]), (m = t[15])),
        (0, C.useEffect)(p, m));
}
function y() {
    (o(document), document.querySelectorAll(`iframe`).forEach(b));
}
function b(e) {
    !x(e) || !e.contentDocument || o(e.contentDocument);
}
function x(e) {
    try {
        return typeof e.contentWindow.location.href == `string`;
    } catch {
        return !1;
    }
}
var S,
    C,
    w = t(() => {
        ((S = i()), (C = e(n(), 1)), _());
    }),
    T = t(() => {});
function E() {
    let e = (0, D.c)(20),
        [t, n] = j(null),
        r;
    (e[0] === n
        ? (r = e[1])
        : ((r = { onKeyDown: n, onKeyUp: n }), (e[0] = n), (e[1] = r)),
        v(r));
    let i = t?.shiftKey ? `⇧ ` : ``,
        a = t?.ctrlKey ? `⌃ ` : ``,
        o = t?.altKey ? `⌥ ` : ``,
        s = t?.metaKey ? `⌘ ` : ``,
        c;
    e[2] !== i || e[3] !== a || e[4] !== o || e[5] !== s
        ? ((c = [i, a, o, s]), (e[2] = i), (e[3] = a), (e[4] = o), (e[5] = s), (e[6] = c))
        : (c = e[6]);
    let l = c.join(``),
        u;
    e[7] === Symbol.for(`react.memo_cache_sentinel`)
        ? ((u = (0, k.jsx)(`span`, { className: `label`, children: `Event:` })),
          (e[7] = u))
        : (u = e[7]);
    let d = t?.type,
        f;
    e[8] === d
        ? (f = e[9])
        : ((f = (0, k.jsxs)(`p`, {
              className: `keyboard-event-row`,
              children: [
                  u,
                  (0, k.jsx)(`input`, { className: `code`, disabled: !0, value: d }),
              ],
          })),
          (e[8] = d),
          (e[9] = f));
    let p;
    e[10] === Symbol.for(`react.memo_cache_sentinel`)
        ? ((p = (0, k.jsx)(`span`, { className: `label`, children: `Key:` })),
          (e[10] = p))
        : (p = e[10]);
    let m = t?.key,
        h;
    e[11] === m
        ? (h = e[12])
        : ((h = (0, k.jsxs)(`p`, {
              className: `keyboard-event-row`,
              children: [
                  p,
                  (0, k.jsx)(`input`, { className: `code`, disabled: !0, value: m }),
              ],
          })),
          (e[11] = m),
          (e[12] = h));
    let g;
    e[13] === Symbol.for(`react.memo_cache_sentinel`)
        ? ((g = (0, k.jsx)(`span`, { className: `label`, children: `Modifiers:` })),
          (e[13] = g))
        : (g = e[13]);
    let _;
    e[14] === l
        ? (_ = e[15])
        : ((_ = (0, k.jsxs)(`p`, {
              className: `keyboard-event-row`,
              children: [g, (0, k.jsx)(`input`, { disabled: !0, value: l })],
          })),
          (e[14] = l),
          (e[15] = _));
    let y;
    return (
        e[16] !== h || e[17] !== _ || e[18] !== f
            ? ((y = (0, k.jsxs)(A, { children: [f, h, _] })),
              (e[16] = h),
              (e[17] = _),
              (e[18] = f),
              (e[19] = y))
            : (y = e[19]),
        y
    );
}
var D, O, k, A, j, M, N, P;
t(() => {
    ((D = i()),
        (O = e(n())),
        w(),
        T(),
        (k = r()),
        ({ Fragment: A, useState: j } = O),
        (M = {
            argTypes: {
                ignoreUsedKeyboardEvents: {
                    control: `boolean`,
                    description: `If the prop is true, the keyboard event target is an input, textarea, or contenteditable element, and the keyboard event is usable by the element, your keyboard event listeners will not be triggered`,
                    table: {
                        defaultValue: { summary: !0 },
                        type: { summary: `boolean` },
                    },
                },
                priority: {
                    control: `number`,
                    description: `Priority defines what order handlers should be invoked and defaults to 0. It can be any number between -50 (lowest priority) and 50 (highest priority).`,
                    table: { defaultValue: { summary: 0 }, type: { summary: `number` } },
                },
                onKeyDown: {
                    action: `onKeyDown`,
                    description: `A function that will be called when a key is pressed down`,
                    table: { type: { summary: `function` } },
                },
                onKeyPress: {
                    action: `onKeyPress`,
                    description: `A function that will be called when a key that produces a character value is pressed down`,
                    table: { type: { summary: `function` } },
                },
                onKeyUp: {
                    action: `onKeyUp`,
                    description: `A function that will be called when a key is released`,
                    table: { type: { summary: `function` } },
                },
            },
            component: E,
            parameters: {
                docs: {
                    description: {
                        component:
                            '`useKeyboardEvents` is a React hook that uses keyboard event listeners on the document to trigger the onKey(Down|Press|Up) functions in order to ensure that all key events are captured regardless of whether there is currently a focused element in the DOM (i.e. `document.activeElement` is set). This solves the problem where keyboard event handlers attached via React’s `onKey(Down|Press|Up)` props miss any keyboard events that occur when the target element and its descendants aren’t focused.',
                    },
                },
            },
            tags: [`autodocs`],
            title: `UIKit/Hooks/useKeyboardEvents`,
        }),
        (N = { args: {} }),
        (N.parameters = {
            ...N.parameters,
            docs: {
                ...N.parameters?.docs,
                source: {
                    originalSource: `{
  args: {}
}`,
                    ...N.parameters?.docs?.source,
                },
            },
        }),
        (P = [`UseKeyboardEvents`]));
})();
export { N as UseKeyboardEvents, P as __namedExportsOrder, M as default };
