import { registerAttributeDirective } from '@melodicdev/core/template';
function parseValue(value) {
    if (typeof value === 'string') {
        return { content: value };
    }
    if (value && typeof value === 'object' && 'content' in value) {
        return value;
    }
    return { content: String(value ?? '') };
}
function tooltipDirective(element, value) {
    if (!value)
        return;
    const { content, placement } = parseValue(value);
    if (!content)
        return;
    // Create ml-tooltip wrapper and move element into it
    const tooltip = document.createElement('ml-tooltip');
    tooltip.setAttribute('content', content);
    if (placement) {
        tooltip.setAttribute('placement', placement);
    }
    element.parentNode?.insertBefore(tooltip, element);
    tooltip.appendChild(element);
    return () => {
        tooltip.parentNode?.insertBefore(element, tooltip);
        tooltip.remove();
    };
}
registerAttributeDirective('tooltip', tooltipDirective);
export { tooltipDirective };
