"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomSortSummary = exports.CustomSortSummaryComponent = 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 CustomSortRedux = tslib_1.__importStar(require("../../Redux/ActionsReducers/CustomSortRedux"));
const ObjectFactory_1 = require("../../Utilities/ObjectFactory");
const ModuleConstants = tslib_1.__importStar(require("../../Utilities/Constants/ModuleConstants"));
const ModuleHeader_1 = require("../Components/ModuleSummary/ModuleHeader");
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 CustomSortWizard_1 = require("./Wizard/CustomSortWizard");
class CustomSortSummaryComponent extends React.Component {
    constructor(props) {
        super(props);
        this.onFinishWizard = (customSort) => {
            if (this.props.CustomSorts.find((x) => x.ColumnId == customSort.ColumnId)) {
                this.props.onEditCustomSort(customSort);
            }
            else {
                this.props.onAddCustomSort(customSort);
            }
            this.setState({
                editedAdaptableObject: null,
                wizardStartIndex: 0,
                wizardStatus: EditableConfigEntityState_1.WizardStatus.None,
            });
        };
        this.state = UIHelper_1.UIHelper.getEmptyConfigState();
    }
    render() {
        const customSort = this.props.CustomSorts.find((c) => c.ColumnId == this.props.summarisedColumn.columnId);
        const columnSortComparer = this.props.api.customSortApi.internalApi.getCustomSortComparer(this.props.summarisedColumn.columnId);
        let noCustomSort = customSort == null && columnSortComparer == null;
        let customSortRow;
        if (!this.props.summarisedColumn.sortable) {
            customSortRow = (React.createElement(ModuleHeader_1.ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: 'Column is not sortable', newButtonDisabled: true, onNew: () => this.onNew(), newButtonTooltip: this.props.moduleInfo.FriendlyName, accessLevel: this.props.accessLevel }));
        }
        else if (noCustomSort) {
            // title row
            customSortRow = (React.createElement(ModuleHeader_1.ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: 'No Custom Sort Set', onNew: () => this.onNew(), accessLevel: this.props.accessLevel, newButtonTooltip: this.props.moduleInfo.FriendlyName }));
        }
        else if (columnSortComparer != null) {
            customSortRow = (React.createElement(ModuleHeader_1.ModuleHeader, { key: this.props.moduleInfo.FriendlyName, moduleInfo: this.props.moduleInfo, moduleSummary: 'Using a Custom Sort Comparer Function', onNew: () => this.onNew(), accessLevel: this.props.accessLevel, newButtonTooltip: this.props.moduleInfo.FriendlyName }));
        }
        else {
            customSortRow = (React.createElement(ModuleDetail_1.ModuleDetail, { key: this.props.moduleInfo.FriendlyName, item1: React.createElement(ModuleProfile_1.ModuleProfile, { moduleInfo: this.props.moduleInfo }), item2: this.getCustomSortedValues(customSort), configEnity: customSort, moduleInfo: this.props.moduleInfo, onEdit: () => this.onEdit(customSort), showEdit: customSort != undefined, onShare: (config) => this.props.onShare(customSort, config), showShare: this.props.teamSharingActivated, onDelete: CustomSortRedux.CustomSortDelete(customSort), showBold: true, accessLevel: this.props.accessLevel }));
        }
        return (React.createElement(React.Fragment, null,
            customSortRow,
            this.state.editedAdaptableObject && (React.createElement(CustomSortWizard_1.CustomSortWizard, { isNew: this.state.wizardStatus === EditableConfigEntityState_1.WizardStatus.New, moduleInfo: this.props.moduleInfo, data: this.state.editedAdaptableObject, configEntities: this.props.CustomSorts, onCloseWizard: () => this.onCloseWizard(), onFinishWizard: this.onFinishWizard }))));
    }
    onNew() {
        let configEntity = ObjectFactory_1.ObjectFactory.CreateEmptyCustomSort();
        configEntity.ColumnId = this.props.summarisedColumn.columnId;
        this.setState({
            editedAdaptableObject: configEntity,
            wizardStartIndex: 1,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.New,
        });
    }
    onEdit(customSort) {
        this.setState({
            editedAdaptableObject: Helper_1.Helper.cloneObject(customSort),
            wizardStartIndex: 1,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.Edit,
        });
    }
    onCloseWizard() {
        this.setState({
            editedAdaptableObject: null,
            wizardStartIndex: 0,
            wizardStatus: EditableConfigEntityState_1.WizardStatus.None,
        });
    }
    canFinishWizard() {
        let customSort = this.state.editedAdaptableObject;
        return (StringExtensions_1.StringExtensions.IsNotNullOrEmpty(customSort.ColumnId) &&
            ArrayExtensions_1.ArrayExtensions.IsNotNullOrEmpty(customSort.SortedValues));
    }
    getCustomSortedValues(customSort) {
        if (ArrayExtensions_1.ArrayExtensions.IsNotNullOrEmpty(customSort.SortedValues)) {
            return customSort.SortedValues.join(', ');
        }
        else {
            return '[Has bespoke Custom Sort implementing using bespoke function]';
        }
    }
}
exports.CustomSortSummaryComponent = CustomSortSummaryComponent;
function mapStateToProps(state, ownProps) {
    return {
        CustomSorts: state.CustomSort.CustomSorts,
    };
}
function mapDispatchToProps(dispatch) {
    return {
        onAddCustomSort: (customSort) => dispatch(CustomSortRedux.CustomSortAdd(customSort)),
        onEditCustomSort: (customSort) => dispatch(CustomSortRedux.CustomSortEdit(customSort)),
        onShare: (entity, config) => dispatch(TeamSharingRedux.TeamSharingShare(entity, ModuleConstants.CustomSortModuleId, config)),
    };
}
exports.CustomSortSummary = (0, react_redux_1.connect)(mapStateToProps, mapDispatchToProps)(CustomSortSummaryComponent);
