package com.atlassian.webdriver.bitbucket.element;

import com.atlassian.pageobjects.elements.PageElement;
import com.google.common.base.Function;
import com.google.common.base.Predicate;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * An option that has one specific field that uniquely identifies the option.
 */
public abstract class IdOption extends Select2.Option {

    public static Function<IdOption, String> TO_ID = IdOption::getId;

    protected IdOption(@Nonnull PageElement container) {
        super(container);
    }

    /**
     * @return unique ID of this option, can be {@code null}, however returning {@code null} will render this option
     * unusable for the purpose of finding/selecting/removing from {@link IdSelect2}.
     */
    @Nullable
    public abstract String getId();

    protected static <O extends IdOption> Predicate<O> idMatches(@Nonnull Class<O> optionClass,
                                                                 @Nonnull final String id) {
        checkNotNull(optionClass, "optionClass");
        checkNotNull(id, "id");

        return option -> id.equals(option.getId());
    }
}
