// Used to support custom (non-state) related dynamic text.
// Example: {custom:glassName}, each key in the array is the value after custom:, like 'glassname'
// Please use lowercase only so that we don't have to worry about case sensitivity.

const customMappings = Object.freeze({
    formattedglassname: [
        { key: 'Windshield Single', transformedValue: 'windshield' },
        { key: 'Windshield Driver', transformedValue: 'driver side split windshield' },
        { key: 'Windshield Passenger', transformedValue: 'passenger side split windshield' },
        { key: 'Rear Stationary', transformedValue: 'rear window' },
        { key: 'Rear Slider', transformedValue: 'rear window' },
        { key: 'Driver Front', transformedValue: 'driver side front door' },
        { key: 'Driver Back', transformedValue: 'driver side back door' },
        { key: 'Driver Vent', transformedValue: 'driver side vent glass' },
        { key: 'Driver Quarter', transformedValue: 'driver side quarter panel' },
        { key: 'Driver SideDoor', transformedValue: 'driver side sliding door' },
        { key: 'Passenger Front', transformedValue: 'passenger side front door' },
        { key: 'Passenger Back', transformedValue: 'passenger side back door' },
        { key: 'Passenger Vent', transformedValue: 'passenger side vent glass' },
        { key: 'Passenger Quarter', transformedValue: 'passenger side quarter panel' },
        { key: 'Passenger SlideDoor', transformedValue: 'passenger side sliding door' }
    ]
});

// Gets an instance of a string where the dynamic portion of the text {custom:KeyName}
// is replaced by a value from the above map.
// If the value isn't found, return the original dynamic string without replacement
const getCustomTransformValue = (dynamicString, key) => {
    // Get array key from the dynamic string
    const regexExp = /{(.*?):(.*?)}/g;
    const matches = [...dynamicString.matchAll(regexExp)];

    if (matches.length === 0) {
        return dynamicString;
    }

    const arrayKey = matches[0][2];

    // Get the array of possible values based on the key name.
    const transformArray = customMappings[arrayKey.toLowerCase()];

    if (transformArray === undefined) {
        return dynamicString;
    }

    // Get the value where the name matches the key name, there should only be one so find() is used.
    const mapObject = transformArray.find((map) => map.key.toLowerCase() === key.toLowerCase());

    if (mapObject === undefined) {
        return dynamicString;
    }

    const finalString = dynamicString.replace(`{custom:${arrayKey}}`, mapObject.transformedValue);

    return finalString;
};

export default getCustomTransformValue;
