package com.atlassian.bitbucket.rest.commit;

import com.atlassian.bitbucket.commit.Commit;
import com.atlassian.bitbucket.commit.MinimalCommit;
import com.atlassian.bitbucket.rest.RestMapEntity;
import com.atlassian.bitbucket.rest.annotation.JsonSurrogate;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.function.Function;

@JsonSerialize
@JsonSurrogate(MinimalCommit.class)
public class RestMinimalCommit extends RestMapEntity {

    public static final Function<MinimalCommit, RestMinimalCommit> REST_TRANSFORM = commit -> {
        if (commit == null) {
            return null;
        }

        return commit instanceof Commit ?
                new RestCommit((Commit) commit) :
                new RestMinimalCommit(commit);
    };

    public RestMinimalCommit(MinimalCommit commit) {
        this(commit.getId(), commit.getDisplayId());
    }

    // Required for REST documentation
    protected RestMinimalCommit(String id, String displayId) {
        put("id", id);
        put("displayId", displayId);
    }

    // REST Documentation Examples

    public static final RestMinimalCommit EXAMPLE = new RestMinimalCommit(
            "abcdef0123abcdef4567abcdef8987abcdef6543",
            "abcdef0"
    );

}
