import * as React from 'react';
import { WizardStatus, } from '../Components/SharedProps/EditableConfigEntityState';
import { connect } from 'react-redux';
import { Helper } from '../../Utilities/Helpers/Helper';
import * as FormatColumnRedux from '../../Redux/ActionsReducers/FormatColumnRedux';
import { ObjectFactory } from '../../Utilities/ObjectFactory';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
import { ModuleHeader } from '../Components/ModuleSummary/ModuleHeader';
import { ModuleDetail } from '../Components/ModuleSummary/ModuleDetail';
import { ModuleProfile } from '../Components/ModuleProfile';
import { StyleVisualItem } from '../Components/StyleVisualItem';
import * as TeamSharingRedux from '../../Redux/ActionsReducers/TeamSharingRedux';
import { UIHelper } from '../UIHelper';
import { FormatColumnWizard } from './Wizard/FormatColumnWizard';
export class FormatColumnSummaryComponent extends React.Component {
    constructor(props) {
        super(props);
        this.onFinishWizard = (formatColumn) => {
            if (this.props.FormatColumns.find((x) => x.Uuid == formatColumn.Uuid)) {
                this.props.onEditFormatColumn(formatColumn);
            }
            else {
                this.props.onAddFormatColumn(formatColumn);
            }
            this.setState({
                editedAdaptableObject: null,
                wizardStartIndex: 0,
                wizardStatus: WizardStatus.None,
            });
        };
        this.state = UIHelper.getEmptyConfigState();
    }
    render() {
        const [formatColumn] = this.props.api.formatColumnApi.getFormatColumnsForColumn(this.props.summarisedColumn);
        let noFormatColumn = formatColumn == null;
        let formatColumnRow;
        if (noFormatColumn) {
            formatColumnRow = (React.createElement(ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: 'No Format Column Set', onNew: () => this.onNew(), newButtonTooltip: this.props.moduleInfo.FriendlyName, accessLevel: this.props.accessLevel }));
        }
        else {
            formatColumnRow = (React.createElement(ModuleDetail, { key: this.props.moduleInfo.FriendlyName, item1: React.createElement(ModuleProfile, { moduleInfo: this.props.moduleInfo }), item2: React.createElement(StyleVisualItem, { Style: formatColumn.Style }), configEnity: formatColumn, showShare: this.props.teamSharingActivated, moduleInfo: this.props.moduleInfo, onEdit: () => this.onEdit(formatColumn), onShare: (config) => this.props.onShare(formatColumn, config), onDelete: FormatColumnRedux.FormatColumnDelete(formatColumn), showBold: true, accessLevel: this.props.accessLevel }));
        }
        return (React.createElement("div", null,
            formatColumnRow,
            this.state.editedAdaptableObject && (React.createElement(FormatColumnWizard, { moduleInfo: this.props.moduleInfo, data: this.state.editedAdaptableObject, configEntities: this.props.FormatColumns, wizardStartIndex: this.state.wizardStartIndex, onCloseWizard: this.onCloseWizard, onFinishWizard: this.onFinishWizard }))));
    }
    onNew() {
        let configEntity = ObjectFactory.CreateEmptyFormatColumn();
        configEntity.Scope = {
            ColumnIds: [this.props.summarisedColumn.columnId],
        };
        this.setState({
            editedAdaptableObject: configEntity,
            wizardStartIndex: 1,
            wizardStatus: WizardStatus.New,
        });
    }
    onEdit(formatColumn) {
        let clonedObject = Helper.cloneObject(formatColumn);
        this.setState({
            editedAdaptableObject: clonedObject,
            wizardStartIndex: 1,
            wizardStatus: WizardStatus.Edit,
        });
    }
    onCloseWizard() {
        this?.setState?.({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: WizardStatus.None,
        });
    }
    canFinishWizard() {
        let formatColumn = this.state.editedAdaptableObject;
        return formatColumn.Scope != undefined && UIHelper.IsNotEmptyStyle(formatColumn.Style);
    }
}
function mapStateToProps(state, ownProps) {
    return {
        FormatColumns: state.FormatColumn.FormatColumns,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onAddFormatColumn: (FormatColumn) => dispatch(FormatColumnRedux.FormatColumnAdd(FormatColumn)),
        onEditFormatColumn: (FormatColumn) => dispatch(FormatColumnRedux.FormatColumnEdit(FormatColumn)),
        onShare: (entity, config) => dispatch(TeamSharingRedux.TeamSharingShare(entity, ModuleConstants.FormatColumnModuleId, config)),
    };
}
export let FormatColumnSummary = connect(mapStateToProps, mapDispatchToProps)(FormatColumnSummaryComponent);
