import * as PopupRedux from '../../Redux/ActionsReducers/PopupRedux';
import { ArrayExtensions } from '../../Utilities/Extensions/ArrayExtensions';
import { CUSTOM_WINDOW_FACTORY_ID } from '../../View/Components/Popups/WindowPopups/WindowPopups';
import { UserInterfaceInternalApi } from '../Internal/UserInterfaceInternalApi';
import { ApiBase } from './ApiBase';
import { ProgressIndicatorHide, ProgressIndicatorShow, } from '../../Redux/ActionsReducers/PopupRedux';
import * as UserInterfaceRedux from '../../Redux/ActionsReducers/UserInterfaceRedux';
export class UserInterfaceApiImpl extends ApiBase {
    internalApi;
    showProgressIndicatorTimeout = null;
    constructor(_adaptable) {
        super(_adaptable);
        this.internalApi = new UserInterfaceInternalApi(_adaptable);
    }
    getColorPalette() {
        let colorPalette = this.getUserInterfaceOptions().colorPalette;
        // first do the function then get hardcoded items
        if (colorPalette != null && typeof colorPalette === 'function') {
            const currentTheme = this.getThemeApi().getCurrentTheme();
            const colours = colorPalette(currentTheme);
            return colours;
        }
        else {
            let arr = colorPalette;
            if (arr && ArrayExtensions.IsNotNullOrEmpty(arr)) {
                return arr;
            }
        }
        return [];
    }
    getCustomIcons() {
        let customIconsProp = this.getUserInterfaceOptions().customIcons;
        if (customIconsProp != null) {
            // first do the function then get hardcoded items
            return typeof customIconsProp === 'function'
                ? customIconsProp()
                : customIconsProp;
        }
        return [];
    }
    getStyleClassNames() {
        return this.getUserInterfaceOptions().styleClassNames;
    }
    getEditableCellStyle() {
        return this.getUserInterfaceOptions().editableCellStyle;
    }
    getEditedCellStyle() {
        return this.getUserInterfaceOptions().editedCellStyle;
    }
    getReadOnlyCellStyle() {
        return this.getUserInterfaceOptions().readOnlyCellStyle;
    }
    getAdaptableObjectTags() {
        const objectTags = this.getUserInterfaceOptions().objectTags;
        if (objectTags != null && typeof objectTags === 'function') {
            return objectTags(this.getAdaptableInternalApi().buildBaseContext());
        }
        else {
            let arr = objectTags;
            if (arr && ArrayExtensions.IsNotNullOrEmpty(arr)) {
                return arr;
            }
        }
    }
    getAdaptableObjectsWithTag(tag, adaptableModule) {
        if (adaptableModule) {
            const module = this.getAdaptableInternalApi()
                .getModuleService()
                .getModuleById(adaptableModule);
            if (!module) {
                return undefined;
            }
            return module
                .getModuleAdaptableObjects()
                ?.filter((ao) => ao.Tags?.includes(tag));
        }
        else {
            const modules = this.getAdaptableInternalApi().getModules();
            let returnObjects = [];
            modules.forEach((module) => {
                returnObjects.push(...module
                    .getModuleAdaptableObjects()
                    ?.filter((ao) => ao.Tags?.includes(tag)));
            });
            return returnObjects;
        }
    }
    showProgressIndicator(config) {
        const close = () => {
            this.hideProgressIndicator();
        };
        if (config.delay) {
            this.showProgressIndicatorTimeout = setTimeout(() => {
                this.dispatchAction(ProgressIndicatorShow(config));
            }, config.delay);
        }
        else {
            this.dispatchAction(ProgressIndicatorShow(config));
        }
        return { close };
    }
    hideProgressIndicator() {
        if (this.showProgressIndicatorTimeout) {
            clearTimeout(this.showProgressIndicatorTimeout);
            this.showProgressIndicatorTimeout = null;
        }
        this.dispatchAction(ProgressIndicatorHide());
    }
    openCustomWindowPopup(config) {
        const close = () => {
            this.dispatchAction(PopupRedux.PopupHideWindow(config.id));
        };
        const action = PopupRedux.PopupShowWindow({
            Id: config.id,
            Title: config.title,
            FactoryId: CUSTOM_WINDOW_FACTORY_ID,
            PopupProps: {
                render: config.render,
                frameworkComponent: config.frameworkComponent,
                onFrameworkComponentDestroyed: config.onFrameworkComponentDestroyed,
                size: config.size,
                position: config.position,
            },
            Icon: config.icon,
        });
        this.dispatchAction(action);
        return { close };
    }
    closeCustomWindowPopup(windowId) {
        this.dispatchAction(PopupRedux.PopupHideWindow(windowId));
    }
    getUserInterfaceState() {
        return this.getAdaptableState().UserInterface;
    }
    isAdaptableUIVisible() {
        return !(this.getUserInterfaceState().HideAdaptableUI ?? false);
    }
    hideAdaptableUI() {
        this.dispatchAction(UserInterfaceRedux.UserInterfaceSetHideAdaptableUI(true));
    }
    showAdaptableUI() {
        this.dispatchAction(UserInterfaceRedux.UserInterfaceSetHideAdaptableUI(false));
    }
}
