import { ok } from "@atcute/client";
import { mainSchema as putRecordSchema } from "@atcute/atproto/types/repo/putRecord";
import type { Nsid, RecordKey } from "@atcute/lexicons/syntax";
import type { OAuthUserAgent } from "@atcute/oauth-browser-client";
import { createClient } from "$lib/auth/agent";
import type { StringRecord } from "./records";

const STRING_COLLECTION = "sh.tangled.string" as Nsid;

// strings live at a generated tid rkey; put upserts the record at `rkey`.
export const putString = async (
	agent: OAuthUserAgent,
	rkey: string,
	record: StringRecord
): Promise<void> => {
	const rpc = createClient(agent);
	await ok(
		rpc.call(putRecordSchema, {
			input: {
				repo: agent.sub,
				collection: STRING_COLLECTION,
				rkey: rkey as RecordKey,
				record
			}
		})
	);
};
