/**
 * Card icon plate helper — builds the ``iconHtml`` / ``iconColor`` /
 * ``iconAttrs`` slots for a mod-card head from any entity that has
 * optional ``icon`` and ``icon_color`` string fields.
 *
 * Usage:
 *
 *     import { makeCardIconFields } from '../core/card-icon.ts';
 *
 *     const mod: ModCardOpts = {
 *       head: {
 *         badge: { text: 'WEATHER · IN' },
 *         name: source.name,
 *         ...makeCardIconFields('weather_source', source.id, source),
 *         // ...
 *       },
 *     };
 *
 * The icon picker is opened by document-level click delegation on
 * ``[data-icon-picker-trigger]``; each feature module registers its
 * adapter via ``registerIconEntityType()`` from ``icon-picker.ts``.
 */

import { renderDeviceIconSvg } from './device-icons.ts';
import type { ModHeadOpts } from './mod-card.ts';

export interface IconableEntity {
    icon?: string;
    icon_color?: string;
}

type IconHeadFields = Pick<ModHeadOpts, 'iconHtml' | 'iconColor' | 'iconAttrs'>;

export function makeCardIconFields(
    entityType: string,
    entityId: string,
    entity: IconableEntity,
    opts: { size?: number } = {},
): IconHeadFields {
    const iconId = entity.icon ?? '';
    const iconColor = entity.icon_color || undefined;
    return {
        iconHtml: iconId ? renderDeviceIconSvg(iconId, { size: opts.size ?? 24 }) : '',
        iconColor,
        iconAttrs: { 'data-icon-picker-trigger': `${entityType}:${entityId}` },
    };
}
