import { isDevMode } from '@angular/core';

export default class WrapperHelper {
    /**
     * Development warning. Showed when environment is set to 'development'.
     */
    public static devWarning(clsName: string, msg: string) : void {
        // @ts-ignore
        if (window.bryntum && window.bryntum.isTestEnv || isDevMode()) {
            console.warn(`Bryntum${clsName}Component development warning!\n${msg}\n` +
            'Please check Angular integration guide: https://bryntum.com/products/scheduler/docs/guide/Scheduler/integration/angular/guide'
            );
        }
    }

    /**
     * Development warning for container parameter.
     */
    public static devWarningContainer(clsName: string, containerParam: string) : void {
        WrapperHelper.devWarning(clsName,
            `Using "${containerParam}" parameter for configuration is not recommended.\n` +
            'Widget is placed automatically inside it\'s container element' +
            `Solution: remove "${containerParam}" parameter from configuration.`
        );
    }

    /**
     * Development warning for config parameter.
     */
    public static devWarningConfigProp(clsName: string, prop: string) : void {
        WrapperHelper.devWarning(clsName,
            `Using "${prop}" parameter for configuration is not recommended.\n` +
            `Solution: Use separate parameter for each "${prop}" value to enable reactive updates of the API instance`
        );
    }

    /**
     * Development warning for config property.
     */
    public static devWarningUpdateProp(clsName: string, prop: string): void {
        WrapperHelper.devWarning(clsName,
            `"${prop}" is a static config option for component constructor only. No runtime changes are supported!`
        );
    }

    /**
     * Applies property value to Bryntum config or instance.
     * @param {Object} configOrInstance target object
     * @param {string} prop property name
     * @param {any} value value
     * @param {Boolean} isConfig config setting mode
     */
    public static applyPropValue(configOrInstance: object|any, prop: string, value: object|any, isConfig: boolean = true): void {

        if (prop === 'project') {
            // Allow use ProjectModel component as project
            if (value && typeof value === 'object') {
                configOrInstance[prop] = value.instance || value;
            }
        }
        else if (prop === 'features' && typeof value === 'object') {
            Object.keys(value).forEach(key => WrapperHelper.applyPropValue(configOrInstance, `${key}Feature`, value[key], isConfig));
        }
        else if (prop === 'config' && typeof value === 'object') {
            Object.keys(value).forEach(key => WrapperHelper.applyPropValue(configOrInstance, key, value[key], isConfig));
        }
        else if (prop === 'columns' && !isConfig) {
            configOrInstance['columns'].data = value;
        }
        else if (prop.endsWith('Feature')) {
            const
                features    = configOrInstance['features'],
                featureName = prop.replace('Feature', '');
            if (isConfig) {
                features[featureName] = value;
            }
            else {
                const feature = features[featureName];
                if (feature) {
                    feature.setConfig(value);
                }
            }
        }
        else {
            configOrInstance[prop] = value;
        }
    }

}
