// what the user has entered so far. the step panes are destroyed and recreated on
// every transition, so this cannot live inside them — the appview has the same
// split for a different reason, keeping progress in the `onboarding` table and the
// profile/keys on the record, so wiring this up later means replacing the module
// rather than moving state around the components.

import { initialKeys, newKey, type MockKey } from "./mock";

export const draft = $state({
	description: "",
	pronouns: "",
	website: "",
	/** a local object url until there is an upload endpoint to post to */
	picture: undefined as string | undefined,
	keys: [...initialKeys] as MockKey[]
});

/** the appview greens Next once any profile field has something in it */
export const profileFilled = () =>
	[draft.description, draft.pronouns, draft.website].some((value) => value.trim() !== "");

// the counter lives here rather than in KeysStep: the pane is recreated on every
// transition, so a component-local counter would restart and collide with the ids
// already in the list
let nextKeyId = 1;

export const addKey = (name: string, key: string) => {
	draft.keys = [...draft.keys, newKey(nextKeyId++, name, key)];
};

export const removeKey = (id: number) => {
	draft.keys = draft.keys.filter((key) => key.id !== id);
};
