/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package leigh.eo.schema.dao;

import leigh.eo.EO;
import leigh.eo.EOLoop;

/**
 * Utility class for interacting with loop elements.
 *
 * @author Alex Leigh
 */
public final class LoopUtil {
    public static String API_NAME = LoopElementDAO.API_TYPE;
    public static String KEY_API_NAME = "apiName";
    public static String KEY_LABEL = "label";

    public static EO create(final String apiName, final String label, final String[] acceptTypes) {
        final EO eo = new EO(API_NAME);
        eo.setValue(KEY_API_NAME, apiName);
        eo.setValue(KEY_LABEL, label);

        final EOLoop ats = LoopElementDAO.getAcceptTypes(eo);

        for (final String s : acceptTypes) {
            final EO accept = AcceptTypeElementDAO.create();
            AcceptTypeElementDAO.setElementApiName(accept, s);
            ats.add(accept);
        }
        return eo;
    }

    public static EO create(final String apiName, final String label) {
        return create(apiName, label, new String[]{});
    }

    /**
     * Returns the first acceptable type for this loop.
     *
     * @param loopSchema
     * @return
     */
    public static String getFirstAcceptType(EO loopSchema) {
        EOLoop acceptTypes = LoopElementDAO.getAcceptTypes(loopSchema);
        return AcceptTypeElementDAO.getElementApiName(acceptTypes.get(0));
    }

}
