import type { components } from "../api/schema";
import { useUpdateAuthority, useDeleteAuthority } from "../api/queries";
import { LabelledRecordRow } from "../components/labelled-record-row";

type AuthorityView = components["schemas"]["AuthorityView"];

export function AuthorityRow({ authority, kind, lang }: { authority: AuthorityView; kind: string; lang: string }) {
  const update = useUpdateAuthority();
  const del = useDeleteAuthority();

  return (
    <LabelledRecordRow
      record={{ ...authority, external_uri: authority.external_uri ?? null }}
      lang={lang}
      deleteConfirmKey="actions.confirmDeleteAuthority"
      savePending={update.isPending}
      saveError={update.error}
      onEditOpen={() => update.reset()}
      onSave={(labels, uri, done) =>
        update.mutate({ id: authority.id, kind, external_uri: uri, labels }, { onSuccess: done })}
      onDelete={() => del.mutateAsync({ id: authority.id, kind })}
    />
  );
}
