/**
 * @license
 * Copyright Google Inc. All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */
import {escapeRegExp} from '@angular/compiler/src/util';
import {serializeNodes} from '../../../src/i18n/digest';
import {MessageBundle} from '../../../src/i18n/message_bundle';
import {Xliff} from '../../../src/i18n/serializers/xliff';
import {HtmlParser} from '../../../src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/ml_parser/interpolation_config';
const HTML = `
not translatable
translatable element with placeholders  {{ interpolation}}
{ count, plural, =0 {test
}}
foo
foo
foo
foo
{ count, plural, =0 { { sex, select, other {
deeply nested
}} }}
Test: { count, plural, =0 { { sex, select, other {
deeply nested
}} } =other {a lot}}
multi
lines
`;
const WRITE_XLIFF = `
  
    
      
        translatable attribute 
        
          file.ts 
          2 
         
       
      
        translatable element  
        
          file.ts 
          3 
         
       
      
        {VAR_PLURAL, plural, =0 { 
        
          file.ts 
          4 
         
       
      
        foo 
        
          file.ts 
          5 
         
        
          file.ts 
          6 
         
        d 
        m 
       
      
        foo 
        
          file.ts 
          7 
         
        d 
        m 
       
      
        foo 
        
          file.ts 
          8 
         
       
      
        
          file.ts 
          9 
         
        ph names 
       
      
        {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other { 
        
          file.ts 
          10 
         
       
      
        Test:  
        
          file.ts 
          11 
         
       
      
        {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other { 
        
          file.ts 
          11 
         
       
      
        multi
lines 
        
          file.ts 
          12 
         
       
    
   
 
`;
const LOAD_XLIFF = `
  
    
      
        translatable attribute 
        etubirtta elbatalsnart 
        
          file.ts 
          1 
         
       
      
        translatable element  
        
          file.ts 
          2 
         
       
      
        {VAR_PLURAL, plural, =0 { 
        {VAR_PLURAL, plural, =0 { 
       
      
        foo 
        oof 
        
          file.ts 
          3 
         
        d 
        m 
       
      
        foo 
        toto 
        
          file.ts 
          4 
         
        d 
        m 
       
      
        foo 
        tata 
        
          file.ts 
          5 
         
       
      
        
          file.ts 
          6 
         
        ph names 
                   
      
        
          file.ts 
          6 
         
        ph names 
       
      
        {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other { 
        {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other { 
       
      
        Test:  
        Test:  
        
          file.ts 
          11 
         
       
      
        {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other { 
        {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other { 
       
      
        multi
lines 
        multi
lignes 
        
          file.ts 
          12 
         
       
      
        First sentence. 
        
          Should not be parsed 
         
        Translated first sentence . 
       
      
        First sentence. Second sentence. 
        
          Should not be parsed 
         
        Translated first  sentence 
       
    
   
 
`;
(function() {
  const serializer = new Xliff();
  function toXliff(html: string, locale: string | null = null): string {
    const catalog = new MessageBundle(new HtmlParser, [], {}, locale);
    catalog.updateFromTemplate(html, 'file.ts', DEFAULT_INTERPOLATION_CONFIG);
    return catalog.write(serializer);
  }
  function loadAsMap(xliff: string): {[id: string]: string} {
    const {i18nNodesByMsgId} = serializer.load(xliff, 'url');
    const msgMap: {[id: string]: string} = {};
    Object.keys(i18nNodesByMsgId)
        .forEach(id => msgMap[id] = serializeNodes(i18nNodesByMsgId[id]).join(''));
    return msgMap;
  }
  describe('XLIFF serializer', () => {
    describe('write', () => {
      it('should write a valid xliff file', () => { expect(toXliff(HTML)).toEqual(WRITE_XLIFF); });
      it('should write a valid xliff file with a source language',
         () => { expect(toXliff(HTML, 'fr')).toContain('file source-language="fr"'); });
    });
    describe('load', () => {
      it('should load XLIFF files', () => {
        expect(loadAsMap(LOAD_XLIFF)).toEqual({
          '983775b9a51ce14b036be72d4cfd65d68d64e231': 'etubirtta elbatalsnart',
          'ec1d033f2436133c14ab038286c4f5df4697484a':
              '
  
    
      
         
    
   
 `;
          expect(() => {
            loadAsMap(XLIFF);
          }).toThrowError(/Message missingtarget misses a translation/);
        });
        it('should throw when a trans-unit has no id attribute', () => {
          const XLIFF = `
  
    
      
         
    
   
 `;
          expect(() => {
            loadAsMap(XLIFF);
          }).toThrowError(/ misses the "id" attribute/);
        });
        it('should throw on duplicate trans-unit id', () => {
          const XLIFF = `
  
    
      
         
      
         
    
   
 `;
          expect(() => {
            loadAsMap(XLIFF);
          }).toThrowError(/Duplicated translations for msg deadbeef/);
        });
      });
      describe('message errors', () => {
        it('should throw on unknown message tags', () => {
          const XLIFF = `
  
    
      
        msg should contain only ph tags  
    
   
 `;
          expect(() => { loadAsMap(XLIFF); })
              .toThrowError(
                  new RegExp(escapeRegExp(`[ERROR ->]msg should contain only ph tags `)));
        });
        it('should throw when a placeholder misses an id attribute', () => {
          const XLIFF = `
  
    
      
         
    
   
 `;
          expect(() => {
            loadAsMap(XLIFF);
          }).toThrowError(new RegExp(escapeRegExp(` misses the "id" attribute`)));
        });
      });
    });
  });
})();