import * as React from 'react';
import { WizardStatus, } from '../Components/SharedProps/EditableConfigEntityState';
import { connect } from 'react-redux';
import { Helper } from '../../Utilities/Helpers/Helper';
import * as FreeTextColumnRedux from '../../Redux/ActionsReducers/FreeTextColumnRedux';
import { ObjectFactory } from '../../Utilities/ObjectFactory';
import * as ModuleConstants from '../../Utilities/Constants/ModuleConstants';
import { ModuleDetail } from '../Components/ModuleSummary/ModuleDetail';
import { ModuleProfile } from '../Components/ModuleProfile';
import * as TeamSharingRedux from '../../Redux/ActionsReducers/TeamSharingRedux';
import { UIHelper } from '../UIHelper';
import { StringExtensions } from '../../Utilities/Extensions/StringExtensions';
import { ArrayExtensions } from '../../Utilities/Extensions/ArrayExtensions';
import { FreeTextColumnWizard } from './Wizard/FreeTextColumnWizard';
export class FreeTextColumnSummaryComponent extends React.Component {
    constructor(props) {
        super(props);
        this.onFinishWizard = (FreeTextColumn) => {
            if (this.props.FreeTextColumns.find((x) => x.ColumnId == FreeTextColumn.ColumnId)) {
                this.props.onEditFreeTextColumn(FreeTextColumn);
            }
            else {
                this.props.onAddFreeTextColumn(FreeTextColumn);
            }
            this.setState({
                editedAdaptableObject: null,
                wizardStartIndex: 0,
                wizardStatus: WizardStatus.None,
            });
        };
        this.state = UIHelper.getEmptyConfigState();
    }
    render() {
        let freeTextColumn = this.props.FreeTextColumns.find((c) => c.ColumnId == this.props.summarisedColumn.columnId);
        let noFreeTextColumn = freeTextColumn == null;
        let FreeTextColumnRow;
        if (!noFreeTextColumn) {
            let description = ArrayExtensions.IsNotEmpty(freeTextColumn.FreeTextStoredValues)
                ? ' Stored values: ' + freeTextColumn.FreeTextStoredValues.length
                : 'No stored values';
            FreeTextColumnRow = (React.createElement(ModuleDetail, { key: this.props.moduleInfo.FriendlyName, item1: React.createElement(ModuleProfile, { moduleInfo: this.props.moduleInfo }), item2: description, configEnity: freeTextColumn, showShare: this.props.teamSharingActivated, moduleInfo: this.props.moduleInfo, onEdit: () => this.onEdit(freeTextColumn), onShare: (config) => this.props.onShare(freeTextColumn, config), onDelete: FreeTextColumnRedux.FreeTextColumnDelete(freeTextColumn), showBold: true, accessLevel: this.props.accessLevel }));
        }
        return (React.createElement("div", null,
            FreeTextColumnRow,
            this.state.editedAdaptableObject && (React.createElement(FreeTextColumnWizard, { moduleInfo: this.props.moduleInfo, data: this.state.editedAdaptableObject, configEntities: this.props.FreeTextColumns, onCloseWizard: () => this.onCloseWizard(), onFinishWizard: this.onFinishWizard }))));
    }
    onNew() {
        let configEntity = ObjectFactory.CreateEmptyFreeTextColumn(this.props.api.gridApi.internalApi.deriveSpecialColumnSettingsFromAgGridDefaultColDef());
        configEntity.ColumnId = this.props.summarisedColumn.columnId;
        this.setState({
            editedAdaptableObject: configEntity,
            wizardStartIndex: 1,
            wizardStatus: WizardStatus.New,
        });
    }
    onEdit(FreeTextColumn) {
        let clonedObject = Helper.cloneObject(FreeTextColumn);
        this.setState({
            editedAdaptableObject: clonedObject,
            wizardStartIndex: 0,
            wizardStatus: WizardStatus.Edit,
        });
    }
    onCloseWizard() {
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: WizardStatus.None,
        });
    }
    canFinishWizard() {
        let FreeTextColumn = this.state.editedAdaptableObject;
        return StringExtensions.IsNotNullOrEmpty(FreeTextColumn.ColumnId);
    }
}
function mapStateToProps(state, ownProps) {
    return {
        FreeTextColumns: state.FreeTextColumn.FreeTextColumns,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onAddFreeTextColumn: (FreeTextColumn) => dispatch(FreeTextColumnRedux.FreeTextColumnAdd(FreeTextColumn)),
        onEditFreeTextColumn: (FreeTextColumn) => dispatch(FreeTextColumnRedux.FreeTextColumnEdit(FreeTextColumn)),
        onShare: (entity, config) => dispatch(TeamSharingRedux.TeamSharingShare(entity, ModuleConstants.FreeTextColumnModuleId, config)),
    };
}
export let FreeTextColumnSummary = connect(mapStateToProps, mapDispatchToProps)(FreeTextColumnSummaryComponent);
