"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.treeFormatter = void 0;
const utils_1 = require("@slickgrid-universal/utils");
const constants_1 = require("../constants");
const formatterUtilities_1 = require("./formatterUtilities");
const utilities_1 = require("../services/utilities");
/** Formatter that must be use with a Tree Data column */
const treeFormatter = (row, cell, value, columnDef, dataContext, grid) => {
    const gridOptions = grid.getOptions();
    const treeDataOptions = gridOptions?.treeDataOptions;
    const indentMarginLeft = treeDataOptions?.indentMarginLeft ?? 15;
    const collapsedPropName = treeDataOptions?.collapsedPropName ?? constants_1.Constants.treeDataProperties.COLLAPSED_PROP;
    const hasChildrenPropName = treeDataOptions?.hasChildrenPropName ?? constants_1.Constants.treeDataProperties.HAS_CHILDREN_PROP;
    const treeLevelPropName = treeDataOptions?.levelPropName ?? constants_1.Constants.treeDataProperties.TREE_LEVEL_PROP;
    let outputValue = value;
    // when a queryFieldNameGetterFn is defined, then get the value from that getter callback function
    outputValue = (0, utilities_1.getCellValueFromQueryFieldGetter)(columnDef, dataContext, value);
    if (outputValue === null || outputValue === undefined || dataContext === undefined) {
        return '';
    }
    if (!dataContext.hasOwnProperty(treeLevelPropName)) {
        throw new Error('[Slickgrid-Universal] You must provide valid "treeDataOptions" in your Grid Options, however it seems that we could not find any tree level info on the current item datacontext row.');
    }
    const treeLevel = dataContext?.[treeLevelPropName] ?? 0;
    const indentSpacerElm = document.createElement('span');
    indentSpacerElm.style.display = 'inline-block';
    indentSpacerElm.style.width = `${indentMarginLeft * treeLevel}px`;
    const slickTreeLevelClass = `slick-tree-level-${treeLevel}`;
    let toggleClass = '';
    if (dataContext[hasChildrenPropName]) {
        toggleClass = dataContext?.[collapsedPropName] ? 'collapsed' : 'expanded'; // parent with child will have a toggle icon
    }
    if (treeDataOptions?.titleFormatter) {
        outputValue = (0, formatterUtilities_1.parseFormatterWhenExist)(treeDataOptions.titleFormatter, row, cell, columnDef, dataContext, grid);
    }
    const spanToggleClass = `slick-group-toggle ${toggleClass}`.trim();
    const spanIconElm = (0, utils_1.createDomElement)('div', { className: spanToggleClass, ariaExpanded: String(toggleClass === 'expanded') });
    const spanTitleElm = (0, utils_1.createDomElement)('span', { className: 'slick-tree-title' });
    grid.applyHtmlCode(spanTitleElm, outputValue);
    spanTitleElm.setAttribute('level', treeLevel);
    const containerElm = gridOptions?.preventDocumentFragmentUsage ? document.createElement('span') : new DocumentFragment();
    containerElm.appendChild(indentSpacerElm);
    containerElm.appendChild(spanIconElm);
    containerElm.appendChild(spanTitleElm);
    return { addClasses: slickTreeLevelClass, html: containerElm };
};
exports.treeFormatter = treeFormatter;
//# sourceMappingURL=treeFormatter.js.map