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 './table/define.js';
import './table/renderer.js';
import './kanban/define.js';
import './kanban/renderer.js';
import { Slot } from '@blockeditor/global/utils';
import { ShadowlessElement, WithDisposable } from '@blockeditor/lit';
import { css, unsafeCSS } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { keyed } from 'lit/directives/keyed.js';
import { createRef } from 'lit/directives/ref.js';
import { html } from 'lit/static-html.js';
import { renderUniLit } from '../_common/components/uni-component/uni-component.js';
import { dataViewCommonStyle } from './common/css-variable.js';
import { viewRendererManager } from './common/data-view.js';
import { DataViewKanbanManager } from './kanban/kanban-view-manager.js';
import { DataViewTableManager } from './table/table-view-manager.js';
const ViewManagerMap = {
    table: DataViewTableManager,
    kanban: DataViewKanbanManager,
};
let DataViewNative = class DataViewNative extends WithDisposable(ShadowlessElement) {
    constructor() {
        super(...arguments);
        this._view = createRef();
        this.viewMap = {};
        this.focusFirstCell = () => {
            this._view.value?.focusFirstCell();
        };
    }
    get expose() {
        return this._view.value;
    }
    connectedCallback() {
        super.connectedCallback();
        this.disposables.add(this.config.selectionUpdated.on(selection => {
            Object.entries(this.viewMap).forEach(([id, v]) => {
                if (!selection || selection.viewId !== id) {
                    v.selectionUpdated.emit(undefined);
                    return;
                }
                v.selectionUpdated.emit(selection);
            });
        }));
        this.disposables.add(this.config.viewSource.updateSlot.on(() => {
            this.requestUpdate();
            this.config.viewSource.views.forEach(v => {
                v.updateSlot.emit();
            });
        }));
    }
    getView(id) {
        if (!this.viewMap[id]) {
            const singleViewSource = this.config.viewSource.viewGet(id);
            const view = new ViewManagerMap[singleViewSource.view.mode](singleViewSource, this.config.dataSource);
            this.viewMap[id] = {
                view: view,
                viewUpdated: singleViewSource.updateSlot,
                selectionUpdated: new Slot(),
                setSelection: selection => {
                    this.config.setSelection(selection);
                },
                handleEvent: (name, handler) => this.config.handleEvent(name, context => {
                    if (this.config.viewSource.currentViewId === id) {
                        return handler(context);
                    }
                }),
                bindHotkey: hotkeys => this.config.bindHotkey(Object.fromEntries(Object.entries(hotkeys).map(([key, fn]) => [
                    key,
                    ctx => {
                        if (this.config.viewSource.currentViewId === id) {
                            return fn(ctx);
                        }
                    },
                ]))),
            };
        }
        return this.viewMap[id];
    }
    renderView(viewData) {
        if (!viewData) {
            return;
        }
        const props = {
            view: viewData.view,
            header: this.config.headerComponent,
            selectionUpdated: viewData.selectionUpdated,
            setSelection: viewData.setSelection,
            bindHotkey: viewData.bindHotkey,
            handleEvent: viewData.handleEvent,
            getFlag: this.config.getFlag,
            onDrag: this.config.onDrag,
            std: this.config.std,
        };
        return keyed(viewData.view.id, renderUniLit(viewRendererManager.getView(viewData.view.type).view, props, { ref: this._view }));
    }
    render() {
        const views = this.config.viewSource.views;
        const viewData = views
            .map(view => this.getView(view.view.id))
            .find(v => v.view.id === this.config.viewSource.currentViewId);
        const containerClass = classMap({
            'toolbar-hover-container': true,
            'data-view-root': true,
        });
        return html `
      <div style="display: contents" class="${containerClass}">
        ${this.renderView(viewData)}
      </div>
    `;
    }
};
DataViewNative.styles = css `
    ${unsafeCSS(dataViewCommonStyle('affine-data-view-native'))}
    affine-data-view-native {
      background-color: var(--affine-background-primary-color);
      display: contents;
    }
  `;
__decorate([
    property({ attribute: false })
], DataViewNative.prototype, "config", void 0);
__decorate([
    state()
], DataViewNative.prototype, "currentView", void 0);
DataViewNative = __decorate([
    customElement('affine-data-view-native')
], DataViewNative);
export { DataViewNative };
//# sourceMappingURL=data-view.js.map