import { h } from './index-b153bfd0.js';
import { C as CSS_SELECTORS, r as renderObjectTypeWithIcon, I as ICONS } from './constants-ae6a35f5.js';
import { f as formatDate } from './helpers-b443f9e9.js';
import { h as hiChar } from './helpers-102277ef.js';
import { c as config } from './config-5a775b7a.js';

// stencil
const BUTTON_ACTION_SELECTOR = "button-action-custom-selector";
const COLUMNS_IDS = {
    action: "action",
    description: "description",
    lastSynchronized: "lastSynchronized",
    localState: "localState",
    modifiedOn: "modifiedOn",
    module: "module",
    name: "name",
    status: "status",
    type: "type"
};
const renderStateCellContent = (action, componentLocale) => {
    let icon;
    let caption;
    let iconColor;
    if (action === "deleted") {
        icon = ICONS.substract;
        caption = componentLocale.states.deleted;
        iconColor = "--color-icon-error-default";
    }
    else if (action === "inserted") {
        icon = ICONS.add;
        caption = componentLocale.states.inserted;
        iconColor = "--color-icon-success-default";
    }
    else if (action === "modified") {
        icon = ICONS.edit;
        caption = componentLocale.states.modified;
        iconColor = "--color-icon-warning-default";
    }
    else if (action === "conflicted") {
        icon = ICONS.warning;
        caption = componentLocale.states.conflicted;
        iconColor = "--color-icon-warning-default";
    }
    else if (!action) {
        // proably is "ignored"
        return null;
    }
    return (h("span", { class: "custom-icon-text-wrapper" },
        h("ch-image", { type: "mask", class: "icon-s", src: icon, style: { backgroundColor: `var(${iconColor})` } }),
        `${caption}`));
};
const renderObjectStatus = (objectId, objectsStatusMap, updatePillAsButton, commitSingleObject, componentLocale) => {
    const status = objectsStatusMap.get(objectId);
    const caption = status === "pending" && updatePillAsButton
        ? componentLocale.status.commit
        : componentLocale.status[status];
    if (status === "committing") {
        return h("span", { class: "spinner-caption" }, caption);
    }
    const classes = `pill pill--${status}`;
    if (updatePillAsButton && status === "pending") {
        return (h("button", { class: {
                "button-secondary": true,
                "button-icon-and-text": true,
                "pill": true,
                "pill--update": true,
                [BUTTON_ACTION_SELECTOR]: true
            }, onClick: commitSingleObject(objectId) }, caption));
    }
    else {
        return h("span", { class: classes }, caption);
    }
};
const renderTabularGridRows = (type, pendingObjectsAfterFilter, ignoredObjectsAfterFilter, alreadyUpdatedObjectsIdsSet, markedObjectsSet, objectsMessagesDataMap, updatePillAsButton, commitSingleObject, objectsStatusMap, searchValue, componentLocale) => {
    let objects;
    if (type === "pending") {
        objects = pendingObjectsAfterFilter;
    }
    else if (type === "ignored") {
        objects = ignoredObjectsAfterFilter;
    }
    searchValue = searchValue.toLowerCase();
    const filteredRows = objects.map(object => {
        const alreadyUpdated = alreadyUpdatedObjectsIdsSet.has(object.id);
        const hideCheckbox = alreadyUpdated || type === "ignored";
        const marked = markedObjectsSet.has(object.id);
        const messages = objectsMessagesDataMap.get(object.id);
        return (h("ch-tabular-grid-row", { class: "tabular-grid-row", key: object.id, rowid: object.id, ref: (el) => marked && (el === null || el === void 0 ? void 0 : el.setAttribute("marked", "true")) },
            h("ch-tabular-grid-cell", { "cell-type": "rich", "row-selector": "true", class: {
                    "tabular-grid-cell": true,
                    "no-checkbox-custom-selector": hideCheckbox
                }, size: "max-content" },
                h("div", null, hiChar(object.name, searchValue))),
            h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, renderObjectTypeWithIcon(object.type)),
            h("ch-tabular-grid-cell", { class: {
                    "tabular-grid-cell": true,
                    [CSS_SELECTORS.GRID_CELL_DESCRIPTION]: true
                } },
                h("div", null, hiChar(object.description, searchValue))),
            h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, formatDate(object.modifiedOn)),
            h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, object.module),
            h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, renderStateCellContent(object.state, componentLocale)),
            h("ch-tabular-grid-cell", { class: "tabular-grid-cell" }, formatDate(object.lastSynchronized)),
            h("ch-tabular-grid-cell", { class: "tabular-grid-cell tabular-grid-cell--for-pill" }, renderObjectStatus(object.id, objectsStatusMap, updatePillAsButton, commitSingleObject, componentLocale)),
            (messages === null || messages === void 0 ? void 0 : messages.length) && (h("ch-tabular-grid-rowset", { class: "tabular-grid-rowset" },
                h("ch-tabular-grid-rowset-empty", { class: {
                        "tabular-grid-rowset-empty": true
                    } },
                    h("ul", { class: "messages-list" }, messages.map(message => {
                        let MESSAGE_ICON;
                        if (message.type === "error") {
                            MESSAGE_ICON = ICONS.error;
                        }
                        else if (message.type === "warning") {
                            MESSAGE_ICON = ICONS.warning;
                        }
                        else if (message.type === "success") {
                            MESSAGE_ICON = ICONS.success;
                        }
                        else {
                            // info
                            MESSAGE_ICON = ICONS.info;
                        }
                        return (h("li", { class: "message-description" },
                            h("ch-image", { class: "icon-s message-icon", src: MESSAGE_ICON }),
                            message.text));
                    })))))));
    });
    return filteredRows;
};
const renderTabularGridColumns = (ignoredGridColumns, gridIsEmpty = false, sortTabularGridColumnCallback, columnSelectorClickedCallback, componentLocale, showRichRowSelector) => {
    const colSize = gridIsEmpty
        ? config.tabularGrid.colSize.auto
        : config.tabularGrid.colSize.maxContent;
    return (h("ch-tabular-grid-columnset", { class: "tabular-grid-column-set" },
        !ignoredGridColumns.includes("name") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.name, columnName: componentLocale.objects.name, columnType: "rich", richRowSelector: showRichRowSelector, richRowSelectorMode: "mark", settingable: false, sortable: true, onColumnSortChanged: sortTabularGridColumnCallback, onColumnSelectorClicked: columnSelectorClickedCallback, size: colSize })),
        !ignoredGridColumns.includes("type") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.type, columnName: componentLocale.objects.type, settingable: false, sortable: true, onColumnSortChanged: sortTabularGridColumnCallback, size: colSize })),
        !ignoredGridColumns.includes("description") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.description, columnName: componentLocale.objects.description, settingable: false, sortable: true, onColumnSortChanged: sortTabularGridColumnCallback, size: config.tabularGrid.colSize.description })),
        !ignoredGridColumns.includes("modifiedOn") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.modifiedOn, columnName: componentLocale.objects.modifiedOn, sortable: true, onColumnSortChanged: sortTabularGridColumnCallback, settingable: false, size: colSize })),
        !ignoredGridColumns.includes("module") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.module, columnName: componentLocale.objects.module, sortable: true, onColumnSortChanged: sortTabularGridColumnCallback, settingable: false, size: colSize })),
        !ignoredGridColumns.includes("localState") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.localState, columnName: componentLocale.objects.localState, sortable: false, settingable: false, size: colSize })),
        !ignoredGridColumns.includes("lastSynchronized") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.lastSynchronized, columnName: componentLocale.objects.lastSynchronized, settingable: false, sortable: true, onColumnSortChanged: sortTabularGridColumnCallback, size: colSize })),
        !ignoredGridColumns.includes("action") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.action, columnName: componentLocale.objects.action, sortable: false, settingable: false, size: colSize })),
        !ignoredGridColumns.includes("status") && (h("ch-tabular-grid-column", { class: "tabular-grid-column", columnId: COLUMNS_IDS.status, columnName: componentLocale.objects.status, sortable: false, settingable: false, size: gridIsEmpty ? "auto" : "100px" }))));
};

export { COLUMNS_IDS as C, renderTabularGridRows as a, renderTabularGridColumns as r };

//# sourceMappingURL=tabular-grid-render-adb3f7e5.js.map