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 { ShadowlessElement, WithDisposable } from '@blockeditor/lit';
import { css } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { createRef } from 'lit/directives/ref.js';
import { renderUniLit } from '../../../_common/components/uni-component/uni-component.js';
let DatabaseCellContainer = class DatabaseCellContainer extends WithDisposable(ShadowlessElement) {
    constructor() {
        super(...arguments);
        this.isEditing = false;
        this.selectCurrentCell = (editing) => {
            if (this.selectionView) {
                this.selectionView.selection = {
                    groupKey: this.groupKey,
                    focus: {
                        rowIndex: this.rowIndex,
                        columnIndex: this.columnIndex,
                    },
                    isEditing: editing,
                };
            }
        };
        this._cell = createRef();
    }
    get selectionView() {
        return this.closest('affine-database-table')?.selectionController;
    }
    get groupKey() {
        return this.closest('affine-data-view-table-group')?.group?.key;
    }
    get readonly() {
        return this.column.readonly;
    }
    get table() {
        const table = this.closest('affine-database-table');
        assertExists(table);
        return table;
    }
    get cell() {
        return this._cell.value;
    }
    connectedCallback() {
        super.connectedCallback();
        this._disposables.addFromEvent(this, 'click', () => {
            if (!this.isEditing) {
                this.selectCurrentCell(!this.column.readonly);
            }
        });
    }
    /* eslint-disable lit/binding-positions, lit/no-invalid-html */
    render() {
        const { edit, view } = this.column.renderer;
        const uni = !this.readonly && this.isEditing && edit != null ? edit : view;
        const props = {
            view: this.view,
            column: this.column,
            rowId: this.rowId,
            isEditing: this.isEditing,
            selectCurrentCell: this.selectCurrentCell,
        };
        return renderUniLit(uni, props, {
            ref: this._cell,
            style: {
                display: 'contents',
            },
        });
    }
};
DatabaseCellContainer.styles = css `
    affine-database-cell-container {
      display: flex;
      align-items: start;
      width: 100%;
      height: 100%;
      border: none;
      outline: none;
    }

    affine-database-cell-container * {
      box-sizing: border-box;
    }

    affine-database-cell-container uni-lit > *:first-child {
      padding: 8px;
    }
  `;
__decorate([
    state()
], DatabaseCellContainer.prototype, "isEditing", void 0);
__decorate([
    property({ attribute: false })
], DatabaseCellContainer.prototype, "view", void 0);
__decorate([
    property({ attribute: false })
], DatabaseCellContainer.prototype, "rowId", void 0);
__decorate([
    property({ attribute: false })
], DatabaseCellContainer.prototype, "rowIndex", void 0);
__decorate([
    property({ attribute: false })
], DatabaseCellContainer.prototype, "columnId", void 0);
__decorate([
    property({ attribute: false })
], DatabaseCellContainer.prototype, "columnIndex", void 0);
__decorate([
    property({ attribute: false })
], DatabaseCellContainer.prototype, "column", void 0);
DatabaseCellContainer = __decorate([
    customElement('affine-database-cell-container')
], DatabaseCellContainer);
export { DatabaseCellContainer };
//# sourceMappingURL=cell-container.js.map