{"version":3,"file":"createAddHook.mjs","names":[],"sources":["../src/createAddHook.ts"],"sourcesContent":["import validateNamespace from './validateNamespace';\nimport validateHookName from './validateHookName';\nimport type { Callback, Hooks, StoreKey } from '.';\nimport type { Handler, HookInfo } from './types';\n\n/**\n *\n * Adds the hook to the appropriate hooks container.\n */\nexport type AddHook = (\n\t/**\n\t * Name of hook to add\n\t */\n\thookName: string,\n\t/**\n\t * The unique namespace identifying the callback in the form.\n\t */\n\tnamespace: string,\n\t/**\n\t * Function to call when the hook is run.\n\t */\n\tcallback: Callback,\n\t/**\n\t * Priority of this hook\n\t */\n\tpriority?: number\n) => void;\n\n/**\n * Returns a function which, when invoked, will add a hook.\n *\n * @param hooks    Hooks instance.\n * @param storeKey\n *\n * @return  Function that adds a new hook.\n */\nfunction createAddHook( hooks: Hooks, storeKey: StoreKey ): AddHook {\n\treturn function addHook( hookName, namespace, callback, priority = 10 ) {\n\t\tconst hooksStore = hooks[ storeKey ];\n\n\t\tif ( ! validateHookName( hookName ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( ! validateNamespace( namespace ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( 'function' !== typeof callback ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error( 'The hook callback must be a function.' );\n\t\t\treturn;\n\t\t}\n\n\t\t// Validate numeric priority\n\t\tif ( 'number' !== typeof priority ) {\n\t\t\t// eslint-disable-next-line no-console\n\t\t\tconsole.error(\n\t\t\t\t'If specified, the hook priority must be a number.'\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tconst handler: Handler = { callback, priority, namespace };\n\n\t\tif ( hooksStore[ hookName ] ) {\n\t\t\t// Find the correct insert index of the new hook.\n\t\t\tconst handlers = hooksStore[ hookName ].handlers;\n\n\t\t\tlet i: number;\n\t\t\tfor ( i = handlers.length; i > 0; i-- ) {\n\t\t\t\tif ( priority >= handlers[ i - 1 ].priority ) {\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif ( i === handlers.length ) {\n\t\t\t\t// If append, operate via direct assignment.\n\t\t\t\thandlers[ i ] = handler;\n\t\t\t} else {\n\t\t\t\t// Otherwise, insert before index via splice.\n\t\t\t\thandlers.splice( i, 0, handler );\n\t\t\t}\n\n\t\t\t// We may also be currently executing this hook.  If the callback\n\t\t\t// we're adding would come after the current callback, there's no\n\t\t\t// problem; otherwise we need to increase the execution index of\n\t\t\t// any other runs by 1 to account for the added element.\n\t\t\thooksStore.__current.forEach( ( hookInfo: HookInfo ) => {\n\t\t\t\tif (\n\t\t\t\t\thookInfo.name === hookName &&\n\t\t\t\t\thookInfo.currentIndex >= i\n\t\t\t\t) {\n\t\t\t\t\thookInfo.currentIndex++;\n\t\t\t\t}\n\t\t\t} );\n\t\t} else {\n\t\t\t// This is the first hook of its type.\n\t\t\thooksStore[ hookName ] = {\n\t\t\t\thandlers: [ handler ],\n\t\t\t\truns: 0,\n\t\t\t};\n\t\t}\n\n\t\tif ( hookName !== 'hookAdded' ) {\n\t\t\thooks.doAction(\n\t\t\t\t'hookAdded',\n\t\t\t\thookName,\n\t\t\t\tnamespace,\n\t\t\t\tcallback,\n\t\t\t\tpriority\n\t\t\t);\n\t\t}\n\t};\n}\n\nexport default createAddHook;\n"],"mappings":";;;;;;;;;;;AAoCA,SAAS,cAAe,OAAc,UAA8B;CACnE,OAAO,SAAS,QAAS,UAAU,WAAW,UAAU,WAAW,IAAK;EACvE,MAAM,aAAa,MAAO;EAE1B,IAAK,CAAE,iBAAkB,QAAS,GACjC;EAGD,IAAK,CAAE,kBAAmB,SAAU,GACnC;EAGD,IAAK,eAAe,OAAO,UAAW;GAErC,QAAQ,MAAO,uCAAwC;GACvD;EACD;EAGA,IAAK,aAAa,OAAO,UAAW;GAEnC,QAAQ,MACP,mDACD;GACA;EACD;EAEA,MAAM,UAAmB;GAAE;GAAU;GAAU;EAAU;EAEzD,IAAK,WAAY,WAAa;GAE7B,MAAM,WAAW,WAAY,SAAU,CAAC;GAExC,IAAI;GACJ,KAAM,IAAI,SAAS,QAAQ,IAAI,GAAG,KACjC,IAAK,YAAY,SAAU,IAAI,EAAG,CAAC,UAClC;GAIF,IAAK,MAAM,SAAS,QAEnB,SAAU,KAAM;QAGhB,SAAS,OAAQ,GAAG,GAAG,OAAQ;GAOhC,WAAW,UAAU,SAAW,aAAwB;IACvD,IACC,SAAS,SAAS,YAClB,SAAS,gBAAgB,GAEzB,SAAS;GAEX,CAAE;EACH,OAEC,WAAY,YAAa;GACxB,UAAU,CAAE,OAAQ;GACpB,MAAM;EACP;EAGD,IAAK,aAAa,aACjB,MAAM,SACL,aACA,UACA,WACA,UACA,QACD;CAEF;AACD"}