import { ColumnRenderer, FILTER_TYPES } from './column.renderer';
export class MetaColumn {
    constructor(table, config = {}, translator) {
        this.table = table;
        this.translator = translator;
        this.cellRenderer = ColumnRenderer.renderCell;
        this.dataType = 'string';
        this.defaultValue = '';
        this.editable = true;
        this.filterable = true;
        this.filterTypes = FILTER_TYPES;
        this.groupKey = 'items';
        this.headerRenderer = ColumnRenderer.renderHeader;
        this.internal = false;
        this.pinnable = true;
        this.pinned = null;
        this.orderable = true;
        this.rules = [];
        this.selectable = true;
        this.sizable = true;
        this.sortable = true;
        this.width = 'auto';
        this.visible = true;
        this.renderer = new ColumnRenderer(this);
        if (typeof config === 'string') {
            this.field = config;
        }
        else {
            Object.assign(this, config);
        }
        if (!this.name) {
            this.name = ColumnRenderer.generateColumnName(this.field, this.caption);
        }
        if (!this.editRenderer) {
            this.editRenderer = ColumnRenderer.renderEdit.bind({ translator: this.translator });
        }
    }
    async contextMenu() {
        return this.renderer.contextMenu();
    }
    async setOffset(offset) {
        return this.renderer.setOffset(offset);
    }
    async setWidth(width) {
        return this.renderer.setWidth(width);
    }
    async initCell(cell, offset) {
        if (this.pinned && !offset && !cell.parentElement) {
            await this.onAttached(cell);
        }
        return this.renderer.initCell(cell, offset);
    }
    /**
     * This little bit of hell fixes https://git.zias.io/ui-foundation/anjuna/issues/738.
     * For some reason, meta columns (and only meta columns, no idea why) consistently get passed to the initCell method before they've attached to the DOM.
     * This breaks the logic for calculating the offset of pinned columns on first render.
     * So what this does is waits for it to attach, and then next time around resolves.
     * If it resolves as soon as it attaches, it doesn't break, but the alignment can be off.
     * I am not proud of this. A more stable fix would probably be to move this into the table after render,
     * but because I don't understand why this isn't a problem for declarative columns, it seems better to bastardize it here for now. -r
     */
    async onAttached(cell) {
        return new Promise(resolve => {
            let attached = false;
            const waiter = window.setInterval(() => {
                if (attached) {
                    window.clearInterval(waiter);
                    resolve();
                }
                else if (cell.parentElement) {
                    attached = true;
                }
            }, 20);
        });
    }
}
