import { Services } from '../../ServiceLocator';

export const SHOW_CONFIRM = 'SHOW_CONFIRM';
export const HIDE_CONFIRM = 'HIDE_CONFIRM';

const DEFAULT_NAME = 'confirmResult';

export const showConfirm = (message, okLabel, cancelLabel, name = DEFAULT_NAME) => async dispatch => {
    dispatch({
        type: SHOW_CONFIRM,
        data: {
            message,
            okLabel,
            cancelLabel,
        },
    });
    return await Services.async.create(name);
};

const ConfirmResultType = 'ConfirmResult';

const createConfirmResult = result => ({ type: ConfirmResultType, result });

export const isConfirmResult = resultObject =>
    typeof resultObject === 'object' && resultObject.type === ConfirmResultType;

export const hideConfirm = (ok, name = DEFAULT_NAME) => dispatch => {
    dispatch({ type: HIDE_CONFIRM });
    ok
        ? Services.async.resolve(name, createConfirmResult(true))
        : Services.async.reject(name, createConfirmResult(false));
};
