import * as React from "react";
import {Command} from "../types";
import {
    insertBefore, insertBreaksAfterSoThatThereIsAnEmptyLineAfter,
    insertBreaksBeforeSoThatThereIsAnEmptyLineBefore,
    selectWordIfCaretIsInsideOne,
} from "../util/MarkdownUtil";
import {buildNewDraftState, getMarkdownStateFromDraftState} from "../util/DraftUtil";

export const quoteCommand: Command = {
    buttonContentBuilder: ({ iconProvider }) => iconProvider("quote-right"),

    buttonProps: { "aria-label": "Insert a quote" },

    execute: (state) => {
        let {text, selection} = getMarkdownStateFromDraftState(state);
        selection = selectWordIfCaretIsInsideOne({text, selection});

        let textInsertion;

        textInsertion = insertBreaksBeforeSoThatThereIsAnEmptyLineBefore({text, selection});
        text = textInsertion.newText;
        selection = textInsertion.newSelection;

        textInsertion = insertBefore(text, "> ", selection, false);
        text = textInsertion.newText;
        selection = textInsertion.newSelection;

        textInsertion = insertBreaksAfterSoThatThereIsAnEmptyLineAfter({text, selection});
        text = textInsertion.newText;
        selection = textInsertion.newSelection;

        return buildNewDraftState(state, {text, selection});
    },
};
