"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreeTextColumnApiImpl = void 0;
const tslib_1 = require("tslib");
const FreeTextColumnRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/FreeTextColumnRedux"));
const ApiBase_1 = require("./ApiBase");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ArrayExtensions_1 = tslib_1.__importDefault(require("../../Utilities/Extensions/ArrayExtensions"));
const FreeTextColumnInternalApi_1 = require("../Internal/FreeTextColumnInternalApi");
class FreeTextColumnApiImpl extends ApiBase_1.ApiBase {
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new FreeTextColumnInternalApi_1.FreeTextColumnInternalApi(_adaptable);
    }
    getFreeTextColumnState() {
        return this.getAdaptableState().FreeTextColumn;
    }
    getFreeTextColumns() {
        return this.getFreeTextColumnState().FreeTextColumns ?? [];
    }
    getFreeTextColumnById(technicalId) {
        return this.getFreeTextColumns().find((ftc) => ftc.Uuid === technicalId);
    }
    getFreeTextColumnForColumnId(columnId) {
        return this.getFreeTextColumns().find((ftc) => ftc.ColumnId === columnId);
    }
    addFreeTextColumn(freeTextColumn) {
        this.addUidToAdaptableObject(freeTextColumn);
        this.dispatchAction(FreeTextColumnRedux.FreeTextColumnAdd(freeTextColumn));
        return this.getFreeTextColumnById(freeTextColumn?.Uuid);
    }
    editFreeTextColumn(freeTextColumn) {
        this.dispatchAction(FreeTextColumnRedux.FreeTextColumnEdit(freeTextColumn));
        return this.getFreeTextColumnById(freeTextColumn?.Uuid);
    }
    setStoredValue(columnId, storedValue) {
        const freeTextColumn = this.getFreeTextColumnForColumnId(columnId);
        if (!freeTextColumn) {
            this.logWarn(`No FreeTextColumn with columnId=${columnId} was found!`);
            return;
        }
        this.dispatchAction(FreeTextColumnRedux.FreeTextColumnAddEditStoredValue(freeTextColumn, storedValue));
        return this.getFreeTextColumnById(freeTextColumn?.Uuid);
    }
    setStoredValues(columnId, storedValues, replaceAction) {
        const freeTextColumn = this.getFreeTextColumnForColumnId(columnId);
        if (freeTextColumn) {
            if (replaceAction == 'All') {
                freeTextColumn.FreeTextStoredValues = storedValues;
            }
            else {
                const existingPKValues = ArrayExtensions_1.default.IsNotNullOrEmpty(freeTextColumn.FreeTextStoredValues)
                    ? freeTextColumn.FreeTextStoredValues.map((fts) => {
                        return fts.PrimaryKey;
                    })
                    : [];
                storedValues.forEach((sv) => {
                    // do summat
                    const isExistingPKValue = existingPKValues.find((epk) => epk == sv.PrimaryKey);
                    if (isExistingPKValue) {
                        if (replaceAction == 'Conflicting') {
                            let existingItem = freeTextColumn.FreeTextStoredValues.find((fsv) => fsv.PrimaryKey == sv.PrimaryKey);
                            if (existingItem) {
                                existingItem.FreeText = sv.FreeText;
                            }
                        }
                        else {
                            freeTextColumn.FreeTextStoredValues.push(sv);
                        }
                    }
                    else {
                        // no existing value so just add it
                        freeTextColumn.FreeTextStoredValues.push(sv);
                    }
                });
            }
            this.dispatchAction(FreeTextColumnRedux.FreeTextColumnEdit(freeTextColumn));
        }
    }
    deleteFreeTextColumn(columnId) {
        let freeTextColumn = this.getFreeTextColumns().find((ftc) => ftc.ColumnId == columnId);
        if (this.checkItemExists(freeTextColumn, columnId, ModuleConstants.FreeTextColumnModuleId)) {
            this.dispatchAction(FreeTextColumnRedux.FreeTextColumnDelete(freeTextColumn));
        }
    }
    openFreeTextColumnSettingsPanel() {
        this.showModulePopup(ModuleConstants.FreeTextColumnModuleId);
    }
    getFreeTextColumnValueForRowNode(freeTextColumn, rowNode) {
        try {
            if (this.getGridApi().isGroupRowNode(rowNode)) {
                return null;
            }
            if (ArrayExtensions_1.default.IsNotNullOrEmpty(freeTextColumn.FreeTextStoredValues)) {
                let pkValue = this.getGridApi().getPrimaryKeyValueForRowNode(rowNode);
                let freeTextStoredValue = freeTextColumn.FreeTextStoredValues.find((fdx) => fdx.PrimaryKey == pkValue);
                if (freeTextStoredValue) {
                    return freeTextStoredValue.FreeText;
                }
            }
            return freeTextColumn.DefaultValue;
        }
        catch (e) {
            this.logError(e);
            return null;
        }
    }
}
exports.FreeTextColumnApiImpl = FreeTextColumnApiImpl;
