import ValidationError from '../error/validation_error';
import {unbundle} from '../util/unbundle_jsonlint';

import type {EnumPropertySpecification} from '../style-spec';

type EnumValidatorOptions = {
    key: string;
    value: unknown;
    valueSpec: EnumPropertySpecification | {values: unknown[] | {[_: string]: unknown}};
};

export default function validateEnum(options: EnumValidatorOptions): ValidationError[] {
    const key = options.key;
    const value = options.value;
    const valueSpec = options.valueSpec;

    const errors: ValidationError[] = [];
    if (Array.isArray(valueSpec.values)) { // <=v7
        if (!valueSpec.values.includes(unbundle(value))) {
            // eslint-disable-next-line @typescript-eslint/no-base-to-string
            errors.push(new ValidationError(key, value, `expected one of [${valueSpec.values.join(', ')}], ${JSON.stringify(value)} found`));
        }
    } else { // >=v8
        if (!Object.keys(valueSpec.values).includes(unbundle(value) as string)) {
            errors.push(new ValidationError(key, value, `expected one of [${Object.keys(valueSpec.values).join(', ')}], ${JSON.stringify(value)} found`));
        }
    }

    return errors;
}
