/**
 * @Copyright (c) Microsoft Corporation. All rights reserved.
 *
 * @file Utils.ts
 */

// Regex that finds { and } so they can be removed on a lookup for string format
const FORMAT_ARGS_REGEX: RegExp = /[\{\}]/g;

// Regex that finds {#} so it can be replaced by the arguments in string format
const FORMAT_REGEX: RegExp = /\{\d+\}/g;

/**
 * String Format is like C# string format.
 * Usage Example: "hello {0}!".format("mike") will return "hello mike!"
 * Calling format on a string with less arguments than specified in the format is invalid
 * Example "I love {0} every {1}".format("CXP") will result in a Debug Exception.
 */
/* tslint:disable:no-any no-null-keyword export-name */
export function format(s: string, ...values: any[]): string {
    'use strict';

    const args: any[] = values;
    // Callback match function
    function replace_func(match: string): any {
        // looks up in the args
        let replacement: any = args[match.replace(FORMAT_ARGS_REGEX, '')];

        // catches undefined in nondebug and null in debug and nondebug
        if (replacement === null) {
            replacement = '';
        }
        return replacement;
    }
    return (s.replace(FORMAT_REGEX, replace_func));
}
/* tslint:enable:no-any no-null-keyword export-name */
