import md5 from "md5";
import { collectAssets, getFolders, remapAssetPath } from "..";

const {
  isImageAsset,
  getNativeFileName,
  propOrIndex,
  reduceArrayResponse,
  fileIsSaved,
} = require("..");

describe("isImageAsset", () => {
  it("returns true if string is a jpg", () => {
    expect(isImageAsset("file.jpg")).toBe(true);
  });

  it("returns true if string is a jpg", () => {
    expect(isImageAsset("file.jpeg")).toBe(true);
  });

  it("returns true if string is a png", () => {
    expect(isImageAsset("file.png")).toBe(true);
  });

  it("returns false otherwise", () => {
    expect(isImageAsset("file.html")).toBe(false);
  });
});

describe("getNativeFileName", () => {
  it("returns the path of a given file", () => {
    const nativePath = "/Native/path";
    const extension = "jpg";
    const url = `https://s3.com/file.${extension}`;
    const path = "path";

    expect(getNativeFileName(nativePath, url, path)).toBe(
      `${nativePath}/${path}/${md5(url)}.${extension}`
    );
  });
});

describe("propOrIndex", () => {
  it("returns a number if the input can be cast to a number", () => {
    expect(propOrIndex(12)).toBe(12);
    expect(propOrIndex("24")).toBe(24);
  });

  it("returns a string otherwise", () => {
    expect(propOrIndex("prop")).toBe("prop");
  });
});

describe("remapAssetPath", () => {
  it("remaps the asset url", () => {
    const config = {
      screen1: {
        asset: "http://img.com/asset.jpg",
        title: "title",
      },
      plugins: {
        plugin_asset: "http://img.com/asset.png",
      },
    };

    const assets = [
      {
        url: "http://img.com/asset.jpg",
        path: ["screen1", "asset"],
        file: "file://asset.jpg",
      },
      {
        url: "http://img.com/asset.png",
        path: ["plugins", "plugin_asset"],
        file: "file://asset.png",
      },
    ];

    const result: Record<string, string> = {
      "http://img.com/asset.jpg": "file://asset.jpg",
      "http://img.com/asset.png": "file://asset.png",
    };

    expect(remapAssetPath(assets, config, result)).toMatchSnapshot();
  });
});

describe("collectAssets", () => {
  it("collects asset information from config files", () => {
    const config = {
      screen1: {
        asset: "http://img.com/asset.jpg",
        title: "title",
      },
      plugins: {
        plugin_asset: "http://img.com/asset.png",
      },
    };

    expect(collectAssets(config, "/nativ/path", "files", [])).toMatchSnapshot();
  });
});

describe("getFolders", () => {
  it("returns the folders in a files list", () => {
    const folder1 = "/path/to/file";
    const folder2 = "/path/to/folder";

    const assets = [
      { file: `${folder1}/file1.jpg` },
      { file: `${folder1}/file2.jpg` },
      { file: `${folder2}/file3.jpg` },
    ];

    const folders = getFolders(assets);

    expect(folders).toHaveProperty("length", 2);
    expect(folders).toEqual([folder1, folder2]);
  });
});

describe("reduceArrayResponse", () => {
  const arrayResponse = [
    { "file://asset1.jpg": true },
    { "file://asset2.jpg": false },
    { "file://asset3.jpg": true },
  ];

  const objectResponse = {
    "file://asset1.jpg": true,
    "file://asset2.jpg": false,
    "file://asset3.jpg": true,
  };

  it("returns the provided response if it's not an array", () => {
    expect(reduceArrayResponse(objectResponse)).toEqual(objectResponse);
  });

  it("reduces the array response into an object", () => {
    expect(reduceArrayResponse(arrayResponse)).toEqual(objectResponse);
  });
});

describe("fileIsSaved", () => {
  const file = "/somepath/to/file.png";

  const iosResult = {
    "file:///somepath/to/file.png": true,
  };

  it("returns true, when file is saved", () => {
    expect(fileIsSaved(iosResult, { file })).toBe(true);
  });
});
