import dedent from 'dedent-js';

/**
 * Tests support for alias options
 * @param {Function} format
 */
export default function supportsAliases(format) {
	it('supports always mode', () => {
		expect(
			format('SELECT a a_column, b AS bColumn FROM ( SELECT * FROM x ) y WHERE z;', {
				aliasAs: 'always',
			})
		).toBe(
			dedent(`
			SELECT
			  a AS a_column,
			  b AS bColumn
			FROM
			  (
			    SELECT
			      *
			    FROM
			      x
			  ) AS y
			WHERE
			  z;
			`)
		);
	});

	it('supports never mode', () => {
		expect(
			format('SELECT a a_column, b AS bColumn FROM ( SELECT * FROM x ) y WHERE z;', {
				aliasAs: 'never',
			})
		).toBe(
			dedent(`
			SELECT
			  a a_column,
			  b bColumn
			FROM
			  (
			    SELECT
			      *
			    FROM
			      x
			  ) y
			WHERE
			  z;
			`)
		);
	});

	it('supports select only mode', () => {
		expect(
			format('SELECT a a_column, b AS bColumn FROM ( SELECT * FROM x ) y WHERE z;', {
				aliasAs: 'select',
			})
		).toBe(
			dedent(`
			SELECT
			  a AS a_column,
			  b AS bColumn
			FROM
			  (
			    SELECT
			      *
			    FROM
			      x
			  ) y
			WHERE
			  z;
			`)
		);
	});

	it('does not format non select clauses', () => {
		expect(format('CREATE TABLE items (a INT PRIMARY KEY, b TEXT);')).toBe(
			'CREATE TABLE items (a INT PRIMARY KEY, b TEXT);'
		);
	});

	it('tabulates alias with aliasAs on', () => {
		const result = format(
			'SELECT alpha AS A, MAX(beta), epsilon E FROM ( SELECT delta AS d, iota i FROM gamma );',
			{ tabulateAlias: true }
		);

		expect(result).toBe(
			dedent`
				SELECT
				  alpha     AS A,
				  MAX(beta),
				  epsilon   AS E
				FROM
				  (
				    SELECT
				      delta AS d,
				      iota  AS i
				    FROM
				      gamma
				  );
			`
		);
	});

	it('accepts tabular alias with aliasAs on', () => {
		const result = format(
			dedent`
				SELECT
				  alpha     AS A,
				  MAX(beta),
				  epsilon   AS E
				FROM
				  (
				    SELECT
				      delta AS d,
				      iota  AS i
				    FROM
				      gamma
				  );
			`
		);

		expect(result).toBe(
			dedent`
				SELECT
				  alpha AS A,
				  MAX(beta),
				  epsilon AS E
				FROM
				  (
				    SELECT
				      delta AS d,
				      iota AS i
				    FROM
				      gamma
				  );
			`
		);
	});

	it('tabulates alias with aliasAs off', () => {
		const result = format(
			'SELECT alpha AS A, MAX(beta), epsilon E FROM ( SELECT delta AS d, iota i FROM gamma );',
			{ tabulateAlias: true, aliasAs: 'never' }
		);

		expect(result).toBe(
			dedent`
				SELECT
				  alpha     A,
				  MAX(beta),
				  epsilon   E
				FROM
				  (
				    SELECT
				      delta d,
				      iota  i
				    FROM
				      gamma
				  );
			`
		);
	});

	it('accepts tabular alias with aliasAs off', () => {
		const result = format(
			dedent`
				SELECT
				  alpha     A,
				  MAX(beta),
				  epsilon   E
				FROM
				  (
				    SELECT
				      delta d,
				      iota  i
				    FROM
				      gamma
				  );
			`
		);

		expect(result).toBe(
			dedent`
				SELECT
				  alpha AS A,
				  MAX(beta),
				  epsilon AS E
				FROM
				  (
				    SELECT
				      delta AS d,
				      iota AS i
				    FROM
				      gamma
				  );
			`
		);
	});
}

@@ -1463,34 +1463,31 @@
 OM ( SELECT 
-delta
+mu
  AS 
-d
+m
 , iota i FRO
@@ -1675,34 +1675,33 @@
 T%0A%09%09%09%09      
-delta
+mu  
  AS 
-d
+m
 ,%0A%09%09%09%09      
@@ -1697,33 +1697,32 @@
 %0A%09%09%09%09      iota 
- 
 AS i%0A%09%09%09%09    FRO
@@ -1962,34 +1962,33 @@
 T%0A%09%09%09%09      
-delta
+mu  
  AS 
-d
+m
 ,%0A%09%09%09%09      
@@ -1992,17 +1992,16 @@
    iota 
- 
 AS i%0A%09%09%09
@@ -2183,34 +2183,31 @@
 T%0A%09%09%09%09      
-delta
+mu
  AS 
-d
+m
 ,%0A%09%09%09%09      
@@ -2401,26 +2401,23 @@
  SELECT 
-delta
+mu
  AS 
-d
+m
 , iota i
@@ -2617,39 +2617,38 @@
 ELECT%0A%09%09%09%09      
-delta d
+mu   m
 ,%0A%09%09%09%09      iota
@@ -2640,33 +2640,32 @@
 %0A%09%09%09%09      iota 
- 
 i%0A%09%09%09%09    FROM%0A%09
@@ -2905,15 +2905,14 @@
     
-delta d
+mu   m
 ,%0A%09%09
@@ -2924,17 +2924,16 @@
    iota 
- 
 i%0A%09%09%09%09  
@@ -3120,18 +3120,15 @@
     
-delta
+mu
  AS 
-d
+m
 ,%0A%09%09

switch delta from unit test as potential keyword