import containerConnect from 'Framework/ComponentContainer';
import { setVisible } from 'App/Reducers/UI/Actions';
import { setCustomTitle } from 'App/Reducers/Configurator/Actions';
import get from 'lodash/get';
import { Services } from 'App/ServiceLocator';
import CustomTitleEditor from '../../Components/CustomTitleEditor';
import Dialog from 'Framework/Components/Dialog';

const mapStateToProps = ({ ui, configurator }) => {
    return {
        Component: CustomTitleEditor,
        DialogComponent: Dialog,
        show: !!ui.visibilityMap.customTitleEditor,
        title: get(configurator.configuration, 'customdata.title') || '',
    };
};

const mapDispatchToProps = dispatch => ({
    onChange: title => {
        dispatch(setCustomTitle(title));
        Services.async.resolve('customEditorConfirm');
    },
    onClose: () => {
        dispatch(setVisible('customTitleEditor', false));
        Services.async.reject('customEditorConfirm', 'Custom title not confirmed.');
    },
});

export default containerConnect(mapStateToProps, mapDispatchToProps);
