const path = require('path');
const fs = require('fs-extra');
const prettier = require('prettier');
const prettierConfig = { semi: false, parser: 'css' };

const buildDir = require('./.boltrc.multi-lang.js').buildDir;

describe('Tests to confirm that the latest upcoming release of the @bolt/build-tools is compiling as expected', () => {
  test('Check that Bolt Build Tools is generating JSON data from Sass files', async () => {
    const dataFileExists = async function() {
      try {
        await fs.promises.access(
          path.resolve(
            process.cwd(),
            `${buildDir}/data/colors/brand.bolt.json`,
          ),
        );
        return true;
      } catch (error) {
        return false;
      }
    };

    expect(await dataFileExists()).toBe(true);
  });

  test('Check JSON data for colors was generated and outputted by the @bolt/build-tools', async () => {
    const getColorData = async function() {
      try {
        await fs.promises.access(
          path.resolve(
            process.cwd(),
            `${buildDir}/data/colors/brand.bolt.json`,
          ),
        );
        const colorData = JSON.parse(
          fs.readFileSync(
            path.resolve(
              process.cwd(),
              `${buildDir}/data/colors/brand.bolt.json`,
            ),
          ),
        );
        return colorData;
      } catch (error) {
        return false;
      }
    };

    const colorData = await getColorData();

    expect(colorData).toMatchSnapshot();

    expect(colorData.indigo.xdark).toBe('rgb(6, 9, 35)');
    expect(colorData.yellow.base).toBe('rgb(255, 204, 77)');
  });

  test('Check that the Japanese-specific CSS bundle was generated and contains the correct font', async () => {
    const getCSSData = async function() {
      try {
        const cssData = fs
          .readFileSync(
            path.resolve(process.cwd(), `${buildDir}/bolt-global-ja.css`),
          )
          .toString();
        return cssData;
      } catch (error) {
        return false;
      }
    };

    const cssFile = await getCSSData();

    const japaneseFontRule =
      'font-family:-apple-system,BlinkMacSystemFont,\"ヒラギノ角ゴ ProN\",\"Hiragino Kaku Gothic ProN\",\"游ゴシック\",\"游ゴシック体\",YuGothic,\"Yu Gothic\",\"メイリオ\",Meiryo,\"ＭＳ ゴシック\",\"MS Gothic\",HiraKakuProN-W3,\"TakaoExゴシック\",TakaoExGothic,MotoyaLCedar,\"Droid Sans Japanese\",sans-serif';

    const englishFontFamilyRule =
      'font-family:"Open Sans","Helvetica Neue",sans-serif;font-family:var(--bolt-font-family-heading)';

    expect(cssFile).toEqual(expect.stringContaining(japaneseFontRule));
    expect(cssFile).toEqual(expect.not.stringContaining(englishFontFamilyRule));

    const prettyCss = await prettier.format(cssFile, prettierConfig);
    expect(prettyCss).toMatchSnapshot();
  });

  test('Check that the Japanese-specific bolt-global-ja.css file built contains the correct utility class styles for the older u-bolt-margin-bottom syntax.', async () => {
    const getCSSData = async function() {
      try {
        const cssData = fs
          .readFileSync(
            path.resolve(process.cwd(), `${buildDir}/bolt-global-ja.css`),
          )
          .toString();
        return cssData;
      } catch (error) {
        return false;
      }
    };

    const cssFile = await getCSSData();
    const prettyCss = await prettier.format(cssFile, prettierConfig);

    expect(prettyCss).toEqual(
      expect.stringContaining(
        await prettier.format(
          '.u-bolt-margin-bottom{margin-bottom:1.155rem!important}',
          prettierConfig,
        ),
      ),
    );
  });

  test('Check that the default English-specific bolt-global.css file built contains the correct utility class styles for the older u-bolt-margin-bottom syntax.', async () => {
    const getCSSData = async function() {
      try {
        const cssData = fs
          .readFileSync(
            path.resolve(process.cwd(), `${buildDir}/bolt-global-en.css`),
          )
          .toString();
        return cssData;
      } catch (error) {
        return false;
      }
    };

    const cssFile = await getCSSData();
    const prettyCss = await prettier.format(cssFile, prettierConfig);

    expect(prettyCss).toEqual(
      expect.stringContaining(
        await prettier.format(
          `.u-bolt-margin-bottom {margin-bottom: 1.65rem !important;}`,
          prettierConfig,
        ),
      ),
    );

    expect(prettyCss).toMatchSnapshot();
  });
});

@@ -2160,17 +2160,16 @@
 temFont,
-%5C
 %22%E3%83%92%E3%83%A9%E3%82%AE%E3%83%8E%E8%A7%92%E3%82%B4 
@@ -2172,20 +2172,18 @@
 %E3%83%8E%E8%A7%92%E3%82%B4 ProN
-%5C
 %22,
-%5C
 %22Hiragin
@@ -2204,30 +2204,25 @@
 ProN
-%5C
 %22,
-%5C
 %22%E6%B8%B8%E3%82%B4%E3%82%B7%E3%83%83%E3%82%AF
-%5C
 %22,
-%5C
 %22%E6%B8%B8%E3%82%B4%E3%82%B7%E3%83%83%E3%82%AF%E4%BD%93
-%5C
 %22,Yu
@@ -2228,17 +2228,16 @@
 uGothic,
-%5C
 %22Yu Goth
@@ -2242,18 +2242,15 @@
 thic
-%5C
 %22,
-%5C
 %22%E3%83%A1%E3%82%A4%E3%83%AA%E3%82%AA
-%5C
 %22,Me
@@ -2258,17 +2258,16 @@
 ryo,
-%5C
 %22%EF%BC%AD%EF%BC%B3 %E3%82%B4%E3%82%B7%E3%83%83%E3%82%AF
 %5C%22,%5C
@@ -2266,12 +2266,10 @@
 %E3%82%B4%E3%82%B7%E3%83%83%E3%82%AF
-%5C
 %22,
-%5C
 %22MS 
@@ -2274,17 +2274,16 @@
 S Gothic
-%5C
 %22,HiraKa
@@ -2292,17 +2292,16 @@
 ProN-W3,
-%5C
 %22TakaoEx
@@ -2304,17 +2304,16 @@
 aoEx%E3%82%B4%E3%82%B7%E3%83%83%E3%82%AF
-%5C
 %22,TakaoE
@@ -2333,17 +2333,16 @@
 aLCedar,
-%5C
 %22Droid S
@@ -2357,9 +2357,8 @@
 nese
-%5C
 %22,sa

update test file to address prettier issue