import { useState } from 'react';
import * as api from '../../plex/api';
import type { Item } from '../../plex/types';
import { useServer } from '../../app/session';
import { invalidateItem, invalidateLibrary, useItem } from '../../app/queries';
import { openDialog } from '../../app/actions';
import { Dialog, toast, toastError } from '../../ui/overlays';
import { Button, PageLoader, Segmented } from '../../ui/primitives';
import { Icon } from '../../ui/Icon';
type Tab = 'general' | 'tags' | 'artwork';
const FIELDS: { key: string; label: string; types: string[]; multiline?: boolean; type?: string }[] = [
{ key: 'title', label: 'Title', types: ['movie', 'show', 'season', 'episode', 'collection'] },
{ key: 'titleSort', label: 'Sort title', types: ['movie', 'show', 'collection'] },
{ key: 'originalTitle', label: 'Original title', types: ['movie', 'show'] },
{ key: 'originallyAvailableAt', label: 'Release date', types: ['movie', 'show', 'episode'], type: 'date' },
{ key: 'contentRating', label: 'Content rating', types: ['movie', 'show', 'collection'] },
{ key: 'studio', label: 'Studio', types: ['movie', 'show'] },
{ key: 'tagline', label: 'Tagline', types: ['movie', 'show'] },
{ key: 'summary', label: 'Summary', types: ['movie', 'show', 'season', 'episode', 'collection'], multiline: true },
];
const TAGS: { key: string; label: string; source: keyof Item; types: string[] }[] = [
{ key: 'genre', label: 'Genres', source: 'genres', types: ['movie', 'show'] },
{ key: 'collection', label: 'Collections', source: 'collections', types: ['movie', 'show'] },
{ key: 'label', label: 'Labels', source: 'labels', types: ['movie', 'show', 'episode', 'season', 'collection'] },
{ key: 'director', label: 'Directors', source: 'directors', types: ['movie', 'episode'] },
{ key: 'writer', label: 'Writers', source: 'writers', types: ['movie', 'episode'] },
{ key: 'country', label: 'Countries', source: 'countries', types: ['movie', 'show'] },
];
function TagEditor({ label, values, onChange, locked, onLock }: { label: string; values: string[]; onChange: (v: string[]) => void; locked: boolean; onLock: () => void }) {
const [draft, setDraft] = useState('');
const add = () => {
const v = draft.trim();
if (v && !values.includes(v)) onChange([...values, v]);
setDraft('');
};
return (
);
}
function LockToggle({ locked, onClick }: { locked: boolean; onClick: () => void }) {
return (
);
}
export function EditMetadataDialog({ item: initial, onClose }: { item: Item; onClose: () => void }) {
const server = useServer();
const full = useItem(initial.id);
const item = full.data ?? initial;
const [tab, setTab] = useState('general');
const [values, setValues] = useState | null>(null);
const [tags, setTags] = useState | null>(null);
const [locks, setLocks] = useState>({});
const [busy, setBusy] = useState(false);
const fields = FIELDS.filter((f) => f.types.includes(item.type));
const tagFields = TAGS.filter((t) => t.types.includes(item.type));
const lockedFields: Record = Object.fromEntries((item.raw?.Field ?? []).map((f: any) => [f.name, !!f.locked]));
if (full.data && !values) {
setValues(Object.fromEntries(fields.map((f) => [f.key, String(item.raw?.[f.key] ?? '')])));
setTags(Object.fromEntries(tagFields.map((t) => [t.key, ((item[t.source] as { tag: string }[]) ?? []).map((x) => x.tag)])));
setLocks(lockedFields);
}
const save = async () => {
if (!values || !tags) return;
setBusy(true);
try {
const changed: Record = {};
for (const f of fields) if ((values[f.key] ?? '') !== String(item.raw?.[f.key] ?? '')) changed[f.key] = values[f.key];
const tagChanges: Record = {};
for (const t of tagFields) {
const before = ((item[t.source] as { tag: string }[]) ?? []).map((x) => x.tag);
const after = tags[t.key];
if (before.join('\u0000') !== after.join('\u0000')) tagChanges[t.key] = { add: after, remove: before.filter((b) => !after.includes(b)) };
}
const lockChanges: Record = {};
for (const [k, v] of Object.entries(locks)) if (!!lockedFields[k] !== v) lockChanges[k] = v;
for (const k of [...Object.keys(changed), ...Object.keys(tagChanges)]) if (!(k in lockChanges)) lockChanges[k] = true;
if (!Object.keys(changed).length && !Object.keys(tagChanges).length && !Object.keys(lockChanges).length) {
onClose();
return;
}
await api.editMetadata(server, item, changed, tagChanges, lockChanges);
toast('Metadata saved', 'success');
invalidateItem(item.id);
invalidateLibrary();
onClose();
} catch (e) {
toastError(e);
} finally {
setBusy(false);
}
};
return (
);
}