import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'title',
})
export class TitlePipe implements PipeTransform {
  readonly placeholder = '...';
  readonly seperator = '-';

  transform(value: [string, string] = ['', ''], max: [number, number] = [24, 24]): any {
    const maxAuthor = max[0];
    let author = (value[0] || '').trim();
    const restAuthor = Math.max(0, maxAuthor - author.length);

    const maxTitle = max[1];
    let title = (value[1] || '').trim();
    const restTitle = Math.max(0, maxTitle - title.length);

    if (author.length > maxAuthor + restTitle) {
      author = author.trim().substr(0, maxAuthor + restTitle - this.placeholder.length) + this.placeholder;
    }

    if (title.length > maxTitle + restAuthor) {
      title = title.trim().substr(0, maxTitle + restAuthor - this.placeholder.length) + this.placeholder;
    }

    let result = author;
    if (result.length > 0) {
      result += ` ${this.seperator} `;
    }
    result += title;

    return result;
  }
}
