import * as FormatColumnRedux from '../../Redux/ActionsReducers/FormatColumnRedux';
import { ApiBase } from './ApiBase';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
import { FormatColumnInternalApi } from '../Internal/FormatColumnInternalApi';
import ArrayExtensions from '../../Utilities/Extensions/ArrayExtensions';
export class FormatColumnApiImpl extends ApiBase {
    internalApi;
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new FormatColumnInternalApi(_adaptable);
    }
    getFormatColumnState() {
        return this.getAdaptableState().FormatColumn;
    }
    getFormatColumns(config) {
        return (this.handleLayoutExtensionObjects(this.getFormatColumnState().FormatColumns, 'FormatColumn', config) ?? []);
    }
    getFormatColumnByUuId(id, config) {
        return this.getFormatColumns(config).find((formatColumn) => formatColumn.Uuid === id);
    }
    getActiveFormatColumns(config) {
        return this.getFormatColumns(config).filter((formatColumn) => !formatColumn.IsSuspended);
    }
    getDisplayFormatForColumn(column, config) {
        const formatColumns = this.getFormatColumnsForColumn(column, config);
        if (ArrayExtensions.IsNullOrEmpty(formatColumns)) {
            return undefined;
        }
        const formatColumn = formatColumns.find((fc) => fc.DisplayFormat != undefined);
        return formatColumn ? formatColumn.DisplayFormat : undefined;
    }
    getSuspendedFormatColumns(config) {
        return this.getFormatColumns(config).filter((formatColumn) => formatColumn.IsSuspended);
    }
    addFormatColumn(formatColumn) {
        this.addUidToAdaptableObject(formatColumn);
        this.dispatchAction(FormatColumnRedux.FormatColumnAdd(formatColumn));
        return this.getFormatColumnByUuId(formatColumn.Uuid);
    }
    addFormatColumns(formatColumns) {
        formatColumns.forEach((fc) => {
            this.addFormatColumn(fc);
        });
        return formatColumns?.map((formatColumn) => this.getFormatColumnByUuId(formatColumn.Uuid));
    }
    editFormatColumn(formatColumn) {
        this.dispatchAction(FormatColumnRedux.FormatColumnEdit(formatColumn));
        return this.getFormatColumnByUuId(formatColumn.Uuid);
    }
    addFormatColumnStyle(formatColumn, style) {
        formatColumn.Style = style;
        this.addFormatColumn(formatColumn);
    }
    updateFormatColumnStyle(formatColumn, style) {
        formatColumn.Style = style;
        this.dispatchAction(FormatColumnRedux.FormatColumnEdit(formatColumn));
    }
    deleteFormatColumn(formatColumn) {
        this.dispatchAction(FormatColumnRedux.FormatColumnDelete(formatColumn));
    }
    deleteAllFormatColumns() {
        this.dispatchAction(FormatColumnRedux.FormatColumnDeleteAll());
    }
    suspendFormatColumn(formatColumn) {
        this.dispatchAction(FormatColumnRedux.FormatColumnSuspend(formatColumn));
        return this.getFormatColumnByUuId(formatColumn.Uuid);
    }
    unSuspendFormatColumn(formatColumn) {
        this.dispatchAction(FormatColumnRedux.FormatColumnUnSuspend(formatColumn));
        return this.getFormatColumnByUuId(formatColumn.Uuid);
    }
    suspendAllFormatColumn() {
        this.dispatchAction(FormatColumnRedux.FormatColumnSuspendAll());
    }
    unSuspendAllFormatColumn() {
        this.dispatchAction(FormatColumnRedux.FormatColumnUnSuspendAll());
    }
    getFormatColumnsForColumn(column, config) {
        const formatColumns = this.getFormatColumns(config).filter((formatColumn) => config?.includeSuspended || !formatColumn.IsSuspended);
        return this.internalApi.getFormatColumnWithColumnInScope(formatColumns, column);
    }
    getFormatColumnsForColumnId(columnId, config) {
        const abColumn = this.getColumnApi().getColumnWithColumnId(columnId);
        if (!abColumn) {
            return [];
        }
        return this.getFormatColumnsForColumn(abColumn, config);
    }
    openFormatColumnSettingsPanel() {
        this.showModulePopup(ModuleConstants.FormatColumnModuleId);
    }
    getFormatColumnsWithAllScope(formatColumns) {
        return formatColumns.filter((fc) => this.getColumnScopeApi().scopeIsAll(fc.Scope));
    }
    getFormatColumnsWithDataTypeScope(formatColumns) {
        return formatColumns.filter((fc) => this.getColumnScopeApi().scopeHasDataType(fc.Scope));
    }
    getFormatColumnsWithColumnScope(formatColumns) {
        return formatColumns.filter((fc) => this.getColumnScopeApi().scopeHasColumns(fc.Scope));
    }
    hasCustomDisplayFormatter(formatColumn) {
        if (!formatColumn || !formatColumn.DisplayFormat || !formatColumn.DisplayFormat.Options) {
            return false;
        }
        const options = formatColumn.DisplayFormat.Options;
        const columnCustomFormatters = options?.CustomDisplayFormats ?? [];
        if (!columnCustomFormatters?.length) {
            return false;
        }
        return true;
    }
    incrementFormatColumnPrecedence(formatColumn) {
        this.dispatchAction(FormatColumnRedux.FormatColumnMoveUp(formatColumn));
    }
    decrementFormatColumnPrecedence(formatColumn) {
        this.dispatchAction(FormatColumnRedux.FormatColumnMoveDown(formatColumn));
    }
    findFormatColumns(criteria) {
        const config = {
            includeLayoutNotExtendedObjects: true,
        };
        return this.getAdaptableInternalApi().findAdaptableObjectsByLookupCriteria(criteria, this.getFormatColumns(config));
    }
}
