/**
 * Generate a unique ID with an optional prefix, similar to React's useId hook.
 */
export function useId(prefix = "dds") {
    const randomStr = Math.random().toString(36).slice(2, 10);
    return `__${prefix}_${randomStr}`;
}
export function inheritAttributes(el, shadowElement, attributes = []) {
    const attributeObject = {};
    // Check for any aria attributes
    for (let i = 0; i < el.attributes.length; i++) {
        const attr = el.attributes[i];
        if (attr.name.includes("aria-")) {
            attributeObject[attr.name] = attr.value;
        }
    }
    // Check for attributes defined by component
    attributes.forEach((attr) => {
        if (el.hasAttribute(attr) || shadowElement?.hasAttribute(attr)) {
            const value = el.getAttribute(attr) ?? shadowElement?.getAttribute(attr) ?? null;
            if (value !== null) {
                attributeObject[attr] = value;
            }
            el.removeAttribute(attr);
        }
    });
    return attributeObject;
}
export function hasSlot(el, name) {
    if (name) {
        return el.querySelector(`:scope > [slot="${name}"]`) !== null;
    }
    return [...el.childNodes].some((node) => {
        if (node.nodeType === Node.TEXT_NODE) {
            return node.textContent?.trim() !== "";
        }
        if (node.nodeType === Node.ELEMENT_NODE) {
            return !node.hasAttribute("slot");
        }
        return false;
    });
}
/**
 * Emit event with logic to cancel HTML events
 * Returns false if event has been prevented
 * @param e - event
 * @param customEvent - custom event
 * @param value - value
 */
export function emitEvent(e, customEvent, value) {
    const event = customEvent.emit(value);
    if (event.defaultPrevented || e.defaultPrevented) {
        e.preventDefault();
        e.stopPropagation();
        e.stopImmediatePropagation();
        return false;
    }
    return true;
}
/**
 * Log validation error for required properties in components
 * @param name - name of the component i.e. <dds-*>
 * @param errorArr - array of attributes with errors
 * @param optionalAttrsArrToRemove - array of optional attributes with errors to be removed from this error message
 */
export function logError(name, errorArr, optionalAttrsArrToRemove) {
    const msg = "Render error, please check required properties.";
    const errors = [...errorArr];
    // remove any potential optional attributes from errors array
    if (optionalAttrsArrToRemove && optionalAttrsArrToRemove.length > 0) {
        for (const optionalAttr of optionalAttrsArrToRemove) {
            if (errors.includes(optionalAttr)) {
                errors.splice(errors.indexOf(optionalAttr), 1);
            }
        }
    }
    console.error(`${name}: ${msg} (${errors})`);
}
/**
 * Log validation error for required properties in components
 * @param errors - array of attributes with errors
 * @param propertyName - name of the property being checked
 * @param property - value of the property being checked
 * @param external - boolean value for an external check on property value
 * @returns modified array of errors
 */
export function handleErrors(errors, propertyName, property, external = false) {
    if ((property && typeof property === "string" && property.trim() === "") ||
        !property ||
        property === "" ||
        external) {
        if (!errors.includes(propertyName)) {
            errors.push(propertyName);
        }
    }
    else if (errors.includes(propertyName)) {
        errors.splice(errors.indexOf(propertyName), 1);
    }
    return errors;
}
