import { DocumentPrototype } from '@interfaces/entities/Document/DocumentPrototype';
import _get from 'lodash/get';

export class ServiceFileNameFactory {
  static fileNameBoundaries = { start: '{', end: '}' };

  static create(
    dataSource: Record<string, unknown>,
    document?: DocumentPrototype
  ): string | undefined {
    let result = '';
    const entries =
      document?.nameTemplate?.split(this.fileNameBoundaries.start) ?? [];
    entries.forEach((e) => {
      if (e.includes(this.fileNameBoundaries.end)) {
        const [dataPath, string] = e.split(this.fileNameBoundaries.end);
        result += _get(dataSource, dataPath, '') + string;
      } else {
        result += e;
      }
    });
    return result || undefined;
  }
}
