import { isInsideBlockByFlavour } from '../../../_common/utils/index.js';
import { getInlineEditorByModel } from '../../../_common/utils/query.js';
export function collectGroupNames(menuItem) {
    return menuItem.reduce((acc, item) => {
        if (!acc.length || acc[acc.length - 1] !== item.groupName) {
            acc.push(item.groupName);
        }
        return acc;
    }, []);
}
export function insertContent(editorHost, model, text, attributes) {
    if (!model.text) {
        throw new Error("Can't insert text! Text not found");
    }
    const inlineEditor = getInlineEditorByModel(editorHost, model);
    if (!inlineEditor) {
        throw new Error("Can't insert text! Inline editor not found");
    }
    const inlineRange = inlineEditor.getInlineRange();
    const index = inlineRange ? inlineRange.index : model.text.length;
    model.text.insert(text, index, attributes);
    // Update the caret to the end of the inserted text
    inlineEditor.setInlineRange({
        index: index + text.length,
        length: 0,
    });
}
export function formatDate(date) {
    // yyyy-mm-dd
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const strTime = `${year}-${month}-${day}`;
    return strTime;
}
export function insideDatabase(model) {
    return isInsideBlockByFlavour(model.page, model, 'affine:database');
}
export function insideDataView(model) {
    return isInsideBlockByFlavour(model.page, model, 'affine:data-view');
}
export function withRemoveEmptyLine(fn) {
    return async (args) => {
        await fn(args);
        const { model } = args;
        if (!model.text?.length) {
            model.page.deleteBlock(model);
        }
    };
}
//# sourceMappingURL=utils.js.map