import { CONSTANTS } from '../interface/exported';

/**
 * @bindable
 * helps get values in attibutes
 *
 */
export function bindable(options?: any) {

    // get passed in options
    let _options: any;
    _options = options;

    return function (target: any, key: any) {

        if (!target[CONSTANTS.META_BINDABLE]) {
            target[CONSTANTS.META_BINDABLE] = {};
        }

        // replace uppercase with lower and add '-' for each one
        const attribute = key.replace(/([a-z])([A-Z])/g, '$1-$2')
            .replace(/\s+/g, '-')
            .toLowerCase();

        // check if attribute is added
        if (options) {
            if (!options.attribute) {
                options.attribute = attribute;
            }
        }

        // check if key is added, if not add
        if (!target[CONSTANTS.META_BINDABLE][key]) {
            target[CONSTANTS.META_BINDABLE][key] = {
                key: key,
                options: _options || {
                    attribute: attribute
                }
            };
        }

    };
}
