"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FreeTextColumnSummary = exports.FreeTextColumnSummaryComponent = void 0;
const tslib_1 = require("tslib");
const React = tslib_1.__importStar(require("react"));
const EditableConfigEntityState_1 = require("../Components/SharedProps/EditableConfigEntityState");
const react_redux_1 = require("react-redux");
const Helper_1 = require("../../Utilities/Helpers/Helper");
const FreeTextColumnRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/FreeTextColumnRedux"));
const ObjectFactory_1 = require("../../Utilities/ObjectFactory");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ModuleDetail_1 = require("../Components/ModuleSummary/ModuleDetail");
const ModuleProfile_1 = require("../Components/ModuleProfile");
const TeamSharingRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/TeamSharingRedux"));
const UIHelper_1 = require("../UIHelper");
const StringExtensions_1 = require("../../Utilities/Extensions/StringExtensions");
const ArrayExtensions_1 = require("../../Utilities/Extensions/ArrayExtensions");
const FreeTextColumnWizard_1 = require("./Wizard/FreeTextColumnWizard");
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: EditableConfigEntityState_1.WizardStatus.None,
            });
        };
        this.state = UIHelper_1.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_1.ArrayExtensions.IsNotEmpty(freeTextColumn.FreeTextStoredValues)
                ? ' Stored values: ' + freeTextColumn.FreeTextStoredValues.length
                : 'No stored values';
            FreeTextColumnRow = (React.createElement(ModuleDetail_1.ModuleDetail, { key: this.props.moduleInfo.FriendlyName, item1: React.createElement(ModuleProfile_1.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_1.FreeTextColumnWizard, { moduleInfo: this.props.moduleInfo, data: this.state.editedAdaptableObject, configEntities: this.props.FreeTextColumns, onCloseWizard: () => this.onCloseWizard(), onFinishWizard: this.onFinishWizard }))));
    }
    onNew() {
        let configEntity = ObjectFactory_1.ObjectFactory.CreateEmptyFreeTextColumn(this.props.api.gridApi.internalApi.deriveSpecialColumnSettingsFromAgGridDefaultColDef());
        configEntity.ColumnId = this.props.summarisedColumn.columnId;
        this.setState({
            editedAdaptableObject: configEntity,
            wizardStartIndex: 1,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.New,
        });
    }
    onEdit(FreeTextColumn) {
        let clonedObject = Helper_1.Helper.cloneObject(FreeTextColumn);
        this.setState({
            editedAdaptableObject: clonedObject,
            wizardStartIndex: 0,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.Edit,
        });
    }
    onCloseWizard() {
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.None,
        });
    }
    canFinishWizard() {
        let FreeTextColumn = this.state.editedAdaptableObject;
        return StringExtensions_1.StringExtensions.IsNotNullOrEmpty(FreeTextColumn.ColumnId);
    }
}
exports.FreeTextColumnSummaryComponent = FreeTextColumnSummaryComponent;
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)),
    };
}
exports.FreeTextColumnSummary = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(FreeTextColumnSummaryComponent);
