"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.treeExportFormatter = void 0;
const utils_1 = require("@slickgrid-universal/utils");
const constants_js_1 = require("../constants.js");
const utilities_js_1 = require("../services/utilities.js");
const formatterUtilities_js_1 = require("./formatterUtilities.js");
/** Formatter that must be use with a Tree Data column */
const treeExportFormatter = (row, cell, value, columnDef, dataContext, grid) => {
    const gridOptions = grid.getOptions();
    const treeDataOptions = gridOptions?.treeDataOptions;
    const collapsedPropName = treeDataOptions?.collapsedPropName ?? constants_js_1.Constants.treeDataProperties.COLLAPSED_PROP;
    const hasChildrenPropName = treeDataOptions?.hasChildrenPropName ?? constants_js_1.Constants.treeDataProperties.HAS_CHILDREN_PROP;
    const treeLevelPropName = treeDataOptions?.levelPropName ?? constants_js_1.Constants.treeDataProperties.TREE_LEVEL_PROP;
    const indentMarginLeft = treeDataOptions?.exportIndentMarginLeft ?? 5;
    const exportIndentationLeadingChar = treeDataOptions?.exportIndentationLeadingChar ?? '.';
    const exportIndentationLeadingSpaceCount = treeDataOptions?.exportIndentationLeadingSpaceCount ?? 3;
    const groupCollapsedSymbol = gridOptions?.excelExportOptions?.groupCollapsedSymbol ?? '⮞';
    const groupExpandedSymbol = gridOptions?.excelExportOptions?.groupExpandedSymbol ?? '⮟';
    let outputValue = value;
    // when a queryFieldNameGetterFn is defined, then get the value from that getter callback function
    outputValue = (0, utilities_js_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;
    let toggleSymbol = '';
    let indentation = 0;
    if (dataContext[hasChildrenPropName]) {
        toggleSymbol = dataContext?.[collapsedPropName] ? groupCollapsedSymbol : groupExpandedSymbol; // parent with child will have a toggle icon
        indentation = treeLevel === 0 ? 0 : indentMarginLeft * treeLevel;
    }
    else {
        indentation = indentMarginLeft * (treeLevel === 0 ? 0 : treeLevel + 1);
    }
    const indentSpacer = (0, utils_1.addWhiteSpaces)(indentation);
    if (treeDataOptions?.titleFormatter) {
        outputValue = (0, formatterUtilities_js_1.parseFormatterWhenExist)(treeDataOptions.titleFormatter, row, cell, columnDef, dataContext, grid);
    }
    const leadingChar = treeLevel === 0 && toggleSymbol
        ? ''
        : treeLevel === 0
            ? `${exportIndentationLeadingChar}${(0, utils_1.addWhiteSpaces)(exportIndentationLeadingSpaceCount)}`
            : exportIndentationLeadingChar;
    outputValue = `${leadingChar}${indentSpacer}${toggleSymbol} ${outputValue}`;
    const sanitizedOutputValue = (0, utils_1.stripTags)(outputValue); // also remove any html tags that might exist
    return sanitizedOutputValue;
};
exports.treeExportFormatter = treeExportFormatter;
//# sourceMappingURL=treeExportFormatter.js.map