// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Gesina Schwalbe
//
// SPDX-License-Identifier: MIT

/**
 * @remarks The render hooks of all classes in the class hierarchy are called, so e.g. for a {@link Dialog}, both the
 * "renderDialog" hook and the "renderApplication" hook are called (in this order).
 */
export function registerForRenderHooks(): void {
    ["renderApplication", "renderActorSheet", "renderItemSheet"].forEach((hook) => {
        Hooks.on(hook, selectTargetInputOnFocus);
    });
}

/**
 * Select the text of input elements in given application when focused via an on focus listener.
 *
 * @param app  - The application in which to activate the listener.
 * @param html - The {@link JQuery} representing the HTML of the application.
 */
function selectTargetInputOnFocus(app: Application, html: JQuery) {
    html.find("input").on("focus", (ev: JQuery.FocusEvent<HTMLInputElement>) => {
        ev.currentTarget.select();
    });
}