var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { assertExists } from '@blockeditor/global/utils';
import { InlineEditor } from '@blockeditor/inline';
import { Text, Workspace } from '@blockeditor/store';
import { css } from 'lit';
import { customElement, property, query } from 'lit/decorators.js';
import { html } from 'lit/static-html.js';
import { getCurrentNativeRange, hasNativeSelection, resetNativeSelection, } from '../../../_common/utils/index.js';
import { tRichText } from '../../logical/data-type.js';
import { BaseCellRenderer } from '../columns/base-cell.js';
const TEXT = 'text/plain';
const styles = css `
  data-view-header-area-text {
    width: 100%;
    display: flex;
  }

  data-view-header-area-text-editing {
    width: 100%;
    display: flex;
    cursor: text;
  }

  .data-view-header-area-rich-text {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 100%;
    height: 100%;
    outline: none;
    word-break: break-all;
    font-size: var(--data-view-cell-text-size);
    line-height: var(--data-view-cell-text-line-height);
  }

  .data-view-header-area-rich-text v-line {
    display: flex !important;
    align-items: center;
    height: 100%;
    width: 100%;
  }

  .data-view-header-area-rich-text v-line > div {
    flex-grow: 1;
  }

  .data-view-header-area-icon {
    height: max-content;
    display: flex;
    align-items: center;
    margin-right: 8px;
    padding: 2px;
    border-radius: 4px;
    margin-top: 2px;
    background-color: var(--affine-background-secondary-color);
  }

  .data-view-header-area-icon svg {
    width: 14px;
    height: 14px;
    fill: var(--affine-icon-color);
    color: var(--affine-icon-color);
  }
`;
export const addHistoryToInlineEditor = (inlineEditor) => {
    let range = null;
    inlineEditor.slots.inlineRangeApply.on(currentRange => {
        range = currentRange;
    });
    const undoManager = new Workspace.Y.UndoManager(inlineEditor.yText, {
        trackedOrigins: new Set([inlineEditor.yText.doc?.clientID]),
    });
    undoManager.on('stack-item-added', (event) => {
        const inlineRange = range && inlineEditor.mounted ? inlineEditor.toInlineRange(range) : null;
        event.stackItem.meta.set('v-range', inlineRange);
    });
    undoManager.on('stack-item-popped', (event) => {
        const inlineRange = event.stackItem.meta.get('v-range');
        if (inlineRange) {
            inlineEditor.setInlineRange(inlineRange);
        }
    });
    undoManager.clear();
    return {
        undoManager,
        handleKeyboardEvent: (e) => {
            if (e instanceof KeyboardEvent &&
                (e.ctrlKey || e.metaKey) &&
                (e.key === 'z' || e.key === 'Z')) {
                e.preventDefault();
                e.stopPropagation();
                if (e.shiftKey) {
                    if (undoManager.canRedo()) {
                        undoManager.redo();
                    }
                }
                else {
                    if (undoManager.canUndo()) {
                        undoManager.undo();
                    }
                }
            }
        },
    };
};
class BaseTextCell extends BaseCellRenderer {
    constructor() {
        super(...arguments);
        this.showIcon = false;
    }
    get titleColumn() {
        const columnId = this.view.header.titleColumn;
        assertExists(columnId);
        return this.view.columnGet(columnId);
    }
    get isRichText() {
        return tRichText.is(this.titleColumn.dataType);
    }
    initInlineEditor(container) {
        const yText = this.getYText(this.titleColumn.getValue(this.rowId));
        const inlineEditor = new InlineEditor(yText);
        this.inlineEditor = inlineEditor;
        inlineEditor.mount(container);
        return inlineEditor;
    }
    initEditingMode(inlineEditor) {
        const historyHelper = addHistoryToInlineEditor(inlineEditor);
        inlineEditor.disposables.addFromEvent(inlineEditor.rootElement, 'keydown', e => {
            historyHelper.handleKeyboardEvent(e);
        });
        inlineEditor.focusEnd();
        this._disposables.add(inlineEditor.slots.inlineRangeUpdate.on(([range]) => {
            if (range) {
                if (!this.isEditing) {
                    this.selectCurrentCell(true);
                }
            }
            else {
                if (this.isEditing) {
                    this.selectCurrentCell(false);
                }
            }
        }));
    }
    getYText(text) {
        if (this.isRichText && (text instanceof Workspace.Y.Text || text == null)) {
            let yText = text;
            if (!yText) {
                yText = new Workspace.Y.Text();
                this.titleColumn?.setValue(this.rowId, yText);
            }
            return yText;
        }
        const yText = new Workspace.Y.Doc().getText('title');
        if (text instanceof Workspace.Y.Text) {
            return text;
        }
        yText.insert(0, text ?? '');
        return yText;
    }
    onExitEditMode() {
        if (!this.isRichText) {
            this.view.cellUpdateValue(this.rowId, this.titleColumn.id, this.isRichText
                ? this.inlineEditor?.yText
                : this.inlineEditor?.yText.toString());
        }
    }
    get icon() {
        const iconColumn = this.view.header.iconColumn;
        if (!iconColumn)
            return;
        return this.view.columnGet(iconColumn).getValue(this.rowId);
    }
    renderIcon() {
        if (!this.showIcon) {
            return;
        }
        const icon = this.icon;
        if (!icon) {
            return;
        }
        return html `<div class="data-view-header-area-icon">${icon}</div>`;
    }
    render() {
        return html ` ${this.renderIcon()}
      <div class="data-view-header-area-rich-text"></div>`;
    }
}
BaseTextCell.styles = styles;
__decorate([
    property({ attribute: false })
], BaseTextCell.prototype, "showIcon", void 0);
__decorate([
    query('.data-view-header-area-rich-text')
], BaseTextCell.prototype, "richText", void 0);
let HeaderAreaTextCell = class HeaderAreaTextCell extends BaseTextCell {
    init() {
        const editor = this.initInlineEditor(this.richText);
        editor.setReadonly(true);
        this._disposables.add({
            dispose: () => {
                editor.unmount();
            },
        });
    }
    firstUpdated() {
        this.init();
    }
    connectedCallback() {
        super.connectedCallback();
        if (this.richText) {
            this.init();
        }
    }
};
HeaderAreaTextCell = __decorate([
    customElement('data-view-header-area-text')
], HeaderAreaTextCell);
export { HeaderAreaTextCell };
let HeaderAreaTextCellEditing = class HeaderAreaTextCellEditing extends BaseTextCell {
    init() {
        const editor = this.initInlineEditor(this.richText);
        this.initEditingMode(editor);
        this._disposables.add({
            dispose: () => {
                editor.unmount();
            },
        });
    }
    firstUpdated() {
        this.init();
    }
    connectedCallback() {
        super.connectedCallback();
        if (this.richText) {
            this.init();
        }
    }
    onCopy(_e) {
        let data = '';
        const range = this.inlineEditor?.getInlineRange();
        if (range) {
            const start = range.index;
            const end = range.index + range.length;
            const value = this.column.getStringValue(this.rowId);
            data = value?.slice(start, end) ?? '';
        }
        // TODO: replace this dom operation
        const rootEl = document.querySelector('editor-host');
        assertExists(rootEl);
        rootEl.std.clipboard
            .writeToClipboard(async (items) => {
            return {
                ...items,
                [TEXT]: data,
            };
        })
            .catch(console.error);
        const savedRange = hasNativeSelection() ? getCurrentNativeRange() : null;
        if (savedRange) {
            resetNativeSelection(savedRange);
        }
    }
    onPaste(e) {
        const textClipboardData = e.clipboardData?.getData(TEXT);
        if (!textClipboardData)
            return;
        const range = this.inlineEditor?.getInlineRange();
        const yText = this.inlineEditor?.yText;
        if (yText) {
            const text = new Text(yText);
            const index = range?.index ?? yText.length;
            if (range?.length) {
                text.replace(range.index, range.length, textClipboardData);
            }
            else {
                text.insert(textClipboardData, index);
            }
            this.inlineEditor?.setInlineRange({
                index: index + textClipboardData.length,
                length: 0,
            });
        }
    }
};
HeaderAreaTextCellEditing = __decorate([
    customElement('data-view-header-area-text-editing')
], HeaderAreaTextCellEditing);
export { HeaderAreaTextCellEditing };
//# sourceMappingURL=text.js.map