/**
 * Copyright Aquera Inc 2025
 *
 * This source code is licensed under the BSD-3-Clause license found in the
 * LICENSE file in the root directory of this source tree.
 */

export function getSlottedElement(shadowRoot: ShadowRoot | null | undefined): HTMLElement | null {
  if (!shadowRoot) return null;
  const slot = shadowRoot.querySelector('slot');
  if (!slot) return null;
  const assignedElements = slot.assignedElements({ flatten: true });
  return (assignedElements[0] as HTMLElement) || null;
}

export function getValueFromSlottedElement(shadowRoot: ShadowRoot | null | undefined): string {
  const slottedElement = getSlottedElement(shadowRoot);
  if (!slottedElement) return '';
  const value = (slottedElement as any).value;
  return value != null ? String(value) : '';
}

export function getDisplayValue(value: string, maxPlaceholderLength: number): string {
  if (value && maxPlaceholderLength > 0 && value.length > maxPlaceholderLength) {
    return value.substring(0, maxPlaceholderLength) + '...';
  }
  return value;
}

export function setSlottedElementValue(shadowRoot: ShadowRoot | null | undefined, value: string): void {
  const slottedElement = getSlottedElement(shadowRoot);
  if (slottedElement && value) {
    (slottedElement as any).value = value;
  }
}

export function updateSlottedElementValue(
  updateComplete: Promise<boolean> | Promise<void>,
  shadowRoot: ShadowRoot | null | undefined,
  value: string
): void {
  updateComplete.then(() => {
    setSlottedElementValue(shadowRoot, value);
  });
}

export function updateValueFromSlottedElement(
  shadowRoot: ShadowRoot | null | undefined,
  currentValue: string
): string {
  const newValue = getValueFromSlottedElement(shadowRoot);
  return newValue !== currentValue ? newValue : currentValue;
}

export function handleDocumentFocusIn(
  event: FocusEvent,
  component: HTMLElement,
  isOpen: boolean
): { shouldClose: boolean; newValue: string | null } {
  if (!isOpen) return { shouldClose: false, newValue: null };
  
  const path = event.composedPath();
  const isWithinComponent = path.includes(component);
  
  if (!isWithinComponent) {
    const shadowRoot = (component as any).shadowRoot;
    const currentValue = (component as any).value || '';
    const newValue = updateValueFromSlottedElement(shadowRoot, currentValue);
    return { shouldClose: true, newValue };
  }
  
  return { shouldClose: false, newValue: null };
}

export function handleDocumentMouseDown(
  event: MouseEvent,
  component: HTMLElement,
  isOpen: boolean
): { shouldClose: boolean; newValue: string | null } {
  if (!isOpen) return { shouldClose: false, newValue: null };
  
  const path = event.composedPath();
  const isWithinComponent = path.includes(component);
  
  if (!isWithinComponent) {
    const shadowRoot = (component as any).shadowRoot;
    const currentValue = (component as any).value || '';
    const newValue = updateValueFromSlottedElement(shadowRoot, currentValue);
    return { shouldClose: true, newValue };
  }
  
  return { shouldClose: false, newValue: null };
}

