const FSWrapper = require('@sisense-ps/fs-wrapper/index');
const CONSTANTS = require('../constants/WindowsConsts');

class ExtensionHelper {
    /**
    * Try to load decorators for existing methods and apply them.
    * @returns {Void}
    */
    static tryApplyDecorators(context) {
        const { PLUGIN_SOURCES_FOLDER, EXTENSION_PATH } = CONSTANTS;

        const extensionPath = FSWrapper.combinePath(
            [
                PLUGIN_SOURCES_FOLDER,
                EXTENSION_PATH
            ]);
        if (!FSWrapper.isPathExistSync(extensionPath)) {
            return;
        }

        const Decorators = require(FSWrapper.combinePath(
            [
                FSWrapper.getRootPath(),
                extensionPath
            ]));

        let decorators = new Decorators();
        Object.getOwnPropertyNames(decorators.__proto__).forEach((decoratorKey) => {
            if (['constructor'].includes(decoratorKey)) {
                return;
            }
            context[decoratorKey] = decorators[decoratorKey](context[decoratorKey], context);
        })
    }
}

module.exports = ExtensionHelper;
