import type { RollTableResultSet } from '../rolltable';
import { Fragment } from 'preact';

export type AttributionSetProps = Pick<RollTableResultSet, "name"|"id"|"global">

export function reconstituteAttributionSetIfExists(element: HTMLParagraphElement | null, partial?: Partial<AttributionSetProps>|null): AttributionSetProps|null {
	if (!element || partial === null) {
		return null
	}
	return reconstituteAttributionSet(element, partial)
}

export function reconstituteAttributionSet(p: HTMLParagraphElement, partial?: Partial<AttributionSetProps>): AttributionSetProps {
	return {
		id: partial?.id ?? parseInt(p.dataset.id!!),
		name: partial?.name ?? p.querySelector<HTMLElement>(".setName")?.innerText ?? null,
		global: partial?.global ?? p.classList.contains('global'),
	}
}

export function AttributionSet({global, name, id}: AttributionSetProps) {
	return <p class={`resultSet${global ? ' global' : ''}`} data-id={id}>
		<span class="setRelation">in {name ? 'the' : 'a'} {global ? 'global' : 'server-local'} set</span>
		{name && <Fragment>{' '}<span class="setName">{name}</span></Fragment>}
	</p>
}
