import { HostListener, Input } from '@angular/core';
import { get, isFunction } from '@bixi/core/utils';
import { ICol, IRow } from '../table.type';
import { hasKey, propGetter, valueGetter } from '../utils';

export class ColBase {
  @Input() row: IRow;
  @Input() col: ICol;
  @Input() index: number;
  @Input() configVersion = new Date().toTimeString();

  @HostListener('click') onClick() {
    if (this.col.onClick && isFunction(this.col.onClick)) {
      this.col.onClick(this.row, this.index);
    }
  }

  get value() {
    return valueGetter(this.row, this.col, this.index);
  }

  get tooltip() {
    const tooltip = get(this.col, 'tooltip');
    if (!hasKey(this.col, 'tooltip') || tooltip === true) {
      return this.value;
    }
    return propGetter(this.row, this.col, this.index, tooltip);
  }

  get style() {
    return {
      display: 'inline-block',
      width: this.col.fixed ? '100%' : this.col.width || 'auto',
      overflow: 'hidden',
      whiteSpace: 'nowrap',
      textOverflow: get(this.col, 'ellipsis', true) ? 'ellipsis' : null
    };
  }
}
