/**
 * 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) {
    if (!shadowRoot)
        return null;
    const slot = shadowRoot.querySelector('slot');
    if (!slot)
        return null;
    const assignedElements = slot.assignedElements({ flatten: true });
    return assignedElements[0] || null;
}
export function getValueFromSlottedElement(shadowRoot) {
    const slottedElement = getSlottedElement(shadowRoot);
    if (!slottedElement)
        return '';
    const value = slottedElement.value;
    return value != null ? String(value) : '';
}
export function getDisplayValue(value, maxPlaceholderLength) {
    if (value && maxPlaceholderLength > 0 && value.length > maxPlaceholderLength) {
        return value.substring(0, maxPlaceholderLength) + '...';
    }
    return value;
}
export function setSlottedElementValue(shadowRoot, value) {
    const slottedElement = getSlottedElement(shadowRoot);
    if (slottedElement && value) {
        slottedElement.value = value;
    }
}
export function updateSlottedElementValue(updateComplete, shadowRoot, value) {
    updateComplete.then(() => {
        setSlottedElementValue(shadowRoot, value);
    });
}
export function updateValueFromSlottedElement(shadowRoot, currentValue) {
    const newValue = getValueFromSlottedElement(shadowRoot);
    return newValue !== currentValue ? newValue : currentValue;
}
export function handleDocumentFocusIn(event, component, isOpen) {
    if (!isOpen)
        return { shouldClose: false, newValue: null };
    const path = event.composedPath();
    const isWithinComponent = path.includes(component);
    if (!isWithinComponent) {
        const shadowRoot = component.shadowRoot;
        const currentValue = component.value || '';
        const newValue = updateValueFromSlottedElement(shadowRoot, currentValue);
        return { shouldClose: true, newValue };
    }
    return { shouldClose: false, newValue: null };
}
export function handleDocumentMouseDown(event, component, isOpen) {
    if (!isOpen)
        return { shouldClose: false, newValue: null };
    const path = event.composedPath();
    const isWithinComponent = path.includes(component);
    if (!isWithinComponent) {
        const shadowRoot = component.shadowRoot;
        const currentValue = component.value || '';
        const newValue = updateValueFromSlottedElement(shadowRoot, currentValue);
        return { shouldClose: true, newValue };
    }
    return { shouldClose: false, newValue: null };
}
//# sourceMappingURL=nile-inline-edit-utils.js.map