import { resolve } from "$app/paths";
import { rkeyFromUri } from "$lib/api/uri";
import type { RepoCardData, PersonData } from "$lib/components/profile/types";
import type * as ShTangledRepoDefs from "$lib/api/lexicons/types/sh/tangled/repo/defs";
import type * as ShTangledActorDefs from "$lib/api/lexicons/types/sh/tangled/actor/defs";

export const toRepoCard = (repo: ShTangledRepoDefs.RepoViewBasic): RepoCardData => ({
	name: repo.slug,
	repoDid: repo.did,
	ownerHandle: repo.owner.handle,
	description: repo.description,
	createdAt: repo.createdAt,
	stars: repo.starCount,
	viewerStarRkey: repo.viewer?.star ? rkeyFromUri(repo.viewer.star) : null
});

export const toPerson = (profile: ShTangledActorDefs.ProfileViewDetailed): PersonData => ({
	did: profile.did,
	handle: profile.handle,
	followers: profile.followersCount,
	following: profile.followsCount,
	viewerFollowRkey: profile.viewer?.following ? rkeyFromUri(profile.viewer.following) : null
});

export const repoHref = (repo: ShTangledRepoDefs.RepoViewBasic) =>
	resolve(`/${repo.owner.handle}/${repo.slug}` as "/");
