const app = document.querySelector('crest-app');

/**
 * going with AppEventDispatcher instead of this class to handle events in a more streamlined way.
 */
// export class BundleSelectEvent extends CustomEvent {
//     constructor(bundle, target) {
//         super('bundle-select', {
//             detail: bundle
//         });
//         //underlying target, but the app is the official target
//         //don't think i really need this, but could be useful for
//         //debugging to see who fired it.
//         this.bundleSelectTarget = target;
//     }

//     fire() {
//         console.log('dispatch');
//         app.dispatchEvent(this);
//     }
// }


/**
 * class to help with apps that we expect to be handled by 
 * the main app (aka app.js). This would include things like
 * loading bundles, or presenting error and info messages to
 * the user.
 */
class AppEventDispatcher {

    fireBundleSelect(bundle) {
        app.dispatchEvent(new CustomEvent(appEvents.bundleSelect, {
            detail: {
                bundle: bundle
            }
        }));
    }

    fireInfoDialog(message) {
        app.dispatchEvent(new CustomEvent(appEvents.infoDialog, {
            detail: {
                message: message
            }
        }));
    }
    
    fireScrollableErrorDialog(message, scrollable) {
        app.dispatchEvent(new CustomEvent(appEvents.scrollableErrorDialog, {
            detail: {
                message: message,
                scrollable: scrollable
            }
        }));   
    }

    fireErrorDialog(message) {
        app.dispatchEvent(new CustomEvent(appEvents.errorDialog, {
            detail: {
                message: message
            }
        }));
    }

    fireErrorYesOrNoDialog(message, callback) {
        app.dispatchEvent(new CustomEvent(appEvents.errorYesOrNoDialog, {
            detail: {
                message: message,
                callback: callback
            }
        }));
    }

    fireInfoToast(message) {
        app.dispatchEvent(new CustomEvent(appEvents.infoToast, {
            detail: {
                message: message
            }
        }));        
    }

    fireErrorToast(message) {
        app.dispatchEvent(new CustomEvent(appEvents.errorToast, {
            detail: {
                message: message
            }
        }));
    }
}

export const appEventDispatcher = new AppEventDispatcher();
export const appEvents = {
    bundleSelect: 'bundle-select',
    infoDialog: 'info-dialog',
    errorDialog: 'error-dialog',
    errorYesOrNoDialog: 'error-yes-or-no-dialog',
    scrollableErrorDialog: 'scrollable-error-dialog',
    infoToast: 'info-toast',
    errorToast: 'error-toast'
};
/*
    admin-bundle
        load from edit screen
        save load from edit screen
        load using icon from list
    
*/