import { onPageLoadComplete } from '@nimius/event-utility';

// The name of the data attribute identifing the target node.
const targetNodeDataAttribute = 'data-example-lazy-initialised-node';

// Wait for page load is completed.
onPageLoadComplete(async () => {
    // Select node for which the script needs to be executed.
    const targetNode = document.querySelector(`[${targetNodeDataAttribute}]`);

    // Only proceed if target node is found in the DOM.
    if (targetNode) {
        /*
         *  The dynamic import part.
         *  Waits until the required script is imported.
         *  Please specify a declarative name for the webpackChunk.
         *  This name will be used in the generated chunk.
         */
        // eslint-disable-next-line max-len
        const { exampleFunction } = await import(/* webpackChunkName: "dynamic-import-example" */ '../dynamic/dynamic-import-target');

        // After the import completed, do the things you want.
        exampleFunction();
    }
});