import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { get } from '@bixi/core/utils';
import { ILinkCol } from '../table.type';
import { hasKey, propGetter } from '../utils';
import { ColBase } from './col.base';

@Component({
  selector: 'bixi-table-col-link',
  host: { '[class.bixi-table-col-link]': 'true' },
  template: `
    <a class="link" [attr.href]="href" nz-tooltip [nzTooltipTitle]="tooltip" [ngStyle]="style" [attr.target]="target"
       (click)="onClick()">{{value}}</a>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class BixiTableColLinkComponent extends ColBase {

  constructor(
    private sanitizer: DomSanitizer
  ) {
    super();
  }

  @Input() col: ILinkCol;

  get target() {
    return get(this.col, 'target', '_self');
  }

  get href() {
    let href;
    if (!hasKey(this.col, 'href')) {
      href = this.value;
    } else {
      href = propGetter(this.row, this.col, this.index, get(this.col, 'href'));
    }
    return this.sanitizer.bypassSecurityTrustUrl(href || 'javascript: void 0');
  }
}


