/**
 * Copyright (c) 2010-2023 Contributors to the openHAB project
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package org.openhab.core.config.core.internal.normalization;

import java.util.ArrayList;
import java.util.List;

/**
 * The normalizer for configuration parameters allowing multiple values. It converts all collections/arrays to a
 * {@link List} and applies the underlying normalizer to each of the values inside that list.
 *
 * @author Simon Kaufmann - Initial contribution
 * @author Thomas Höfer - made class final and minor javadoc changes
 */
final class ListNormalizer extends AbstractNormalizer {

    private final Normalizer delegate;

    ListNormalizer(Normalizer delegate) {
        this.delegate = delegate;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public Object doNormalize(Object value) {
        if (!isList(value)) {
            List ret = new ArrayList(1);
            ret.add(delegate.normalize(value));
            return ret;
        }
        if (isArray(value)) {
            List ret = new ArrayList(((Object[]) value).length);
            for (Object object : ((Object[]) value)) {
                ret.add(delegate.normalize(object));
            }
            return ret;
        }
        if (value instanceof List list) {
            List ret = new ArrayList(list.size());
            for (Object object : list) {
                ret.add(delegate.normalize(object));
            }
            return ret;
        }
        if (value instanceof Iterable iterable) {
            List ret = new ArrayList();
            for (Object object : iterable) {
                ret.add(delegate.normalize(object));
            }
            return ret;
        }
        return value;
    }

    static boolean isList(Object value) {
        return isArray(value) || value instanceof Iterable;
    }

    private static boolean isArray(Object object) {
        return object != null && object.getClass().isArray();
    }
}
