// #region Imports
import { html } from '../../../../Dom/Html';
import { repeat, when } from '../../../Directives/Directives';
// #endregion
/**
 * The template of the `DataListElement`.
 *
 * @public
 */
export function dataListElementTemplate(e) {
    return html `
        <mosaik-list part="list">
            ${repeat(e.source, (item) => item, (item) => html `
                <mosaik-list-item>
                ${repeat(e.getItemDefinitions(), (def) => def.key, (def) => html `
                    ${when(def.itemTemplate, () => def.itemTemplate(item, def, resolveItemValue(item, def)), () => html `<mosaik-text .text="${resolveItemValue(item, def)}"></mosaik-text>`)}
                `)}
                </mosaik-list-item>
            `)}
        </mosaik-list>
    `;
}
/**
 * Resolves the display value for an item definition from a data item.
 * Supports both a dot-separated string path and a value getter callback function.
 *
 * @param item - The data item for the current row.
 * @param definition - The item definition.
 * @returns The resolved string value, or an empty string if no path or function is provided.
 *
 * @private
 */
function resolveItemValue(item, definition) {
    const pathOrFn = definition.pathFn;
    if (!pathOrFn) {
        return '';
    }
    if (typeof pathOrFn === 'function') {
        const value = pathOrFn(item);
        return value !== undefined && value !== null ? String(value) : '';
    }
    return getObjectValueByPath(item, pathOrFn)?.toString() ?? '';
}
/**
 * Retrieves the value of a nested property from an object using a dot-separated path.
 * Supports array traversal via `[]` segments (e.g., `approvals.[].approvedBy`).
 *
 * @param obj - The object from which to retrieve the value.
 * @param path - The dot-separated path to the property.
 * @returns The value of the nested property, or `undefined` if any part of the path is invalid.
 *
 * @private
 */
function getObjectValueByPath(obj, path) {
    if (!path) {
        return obj;
    }
    const parts = path.split('.');
    let current = obj;
    for (let i = 0; i < parts.length; i++) {
        const part = parts[i];
        if (part === '[]') {
            if (!Array.isArray(current)) {
                return undefined;
            }
            const remainingPath = parts.slice(i + 1).join('.');
            const values = current
                .map((item) => getObjectValueByPath(item, remainingPath))
                .filter((v) => v !== undefined && v !== null);
            return values.join(', ');
        }
        if (current && typeof current === 'object' && part in current) {
            current = current[part];
        }
        else {
            return undefined;
        }
    }
    return current;
}
//# sourceMappingURL=DataListElementTemplate.js.map