import * as utils from "../configurationUtils";

const { modifyDefaultConfigValues, parseLanguageTracks } = utils;

const configuration = {
  general: { fields: [{ key: "general_1", initial_value: true }] },
  styles: { fields: [{ key: "styles_1", initial_value: true }] },
  localizations: {
    fields: [{ key: "localizations_1", initial_value: true, label: "label" }],
  },
  custom_configuration_fields: [
    { key: "custom_configuration_fields_1", initial_value: true },
    { key: "custom_configuration_fields_2", initial_value: "black" },
    {
      group: true,
      fields: [{ key: "custom_configuration_fields_3", initial_value: "red" }],
    },
  ],
};

describe("utilities", () => {
  describe("modifyDefaultConfigValues", () => {
    it("should not modify the configuration if a map key doesn't exist in configuration ", function () {
      const res = modifyDefaultConfigValues(configuration, {
        custom_configuration_fields: {
          non_existing_key: { initial_value: "true" },
        },
      });

      expect(res).toEqual(configuration);
    });

    it("should modify the key from mapping", function () {
      const currentResult = modifyDefaultConfigValues(configuration, {
        styles: { styles_1: { initial_value: false } },
      });

      expect(currentResult.styles.fields[0].initial_value).toBe(false);
    });

    it("should add extra keys the key from mapping", function () {
      const currentResult = modifyDefaultConfigValues(configuration, {
        styles: { styles_1: { extra_key: false } },
      });

      expect(currentResult.styles.fields[0].extra_key).toBeDefined();
    });

    it("should not remove existing keys that aren't being overwritten", function () {
      const currentResult = modifyDefaultConfigValues(configuration, {
        localizations: { localizations_1: { initial_value: false } },
      });

      expect(currentResult.localizations.fields[0].label).toBeDefined();
    });

    it("should modify values in groups as well", function () {
      const currentResult = modifyDefaultConfigValues(configuration, {
        custom_configuration_fields: {
          custom_configuration_fields_3: { initial_value: "blue" },
        },
      });

      expect(
        currentResult.custom_configuration_fields[2].fields[0].initial_value
      ).toBe("blue");
    });
  });

  describe("parseLanguageTracks", () => {
    const textTrack = { index: 0, id: "text-0" };
    const audioTrack = { index: 0, id: "audio-0" };

    it("should handle undefined tracks", () => {
      const callParseLanguage = () => {
        parseLanguageTracks({});
      };

      expect(callParseLanguage).not.toThrow();
    });

    it("should handle stringified json and object tracks", () => {
      const currentResultWithObj = parseLanguageTracks({
        textTracks: [textTrack],
        audioTracks: [audioTrack],
      });

      const currentResultWithString = parseLanguageTracks({
        textTracks: JSON.stringify([textTrack]),
        audioTracks: JSON.stringify([audioTrack]),
      });

      expect(currentResultWithObj.textTracks.length).toBe(1);
      expect(currentResultWithObj.audioTracks.length).toBe(1);

      expect(currentResultWithString.textTracks.length).toBe(1);
      expect(currentResultWithString.audioTracks.length).toBe(1);
    });

    it("should add textType property to tracks", () => {
      const currentResult = parseLanguageTracks({
        textTracks: [textTrack],
        audioTracks: [audioTrack],
      });

      expect(currentResult.textTracks[0]).toHaveProperty("trackType");
      expect(currentResult.audioTracks[0]).toHaveProperty("trackType");
    });

    it("should correctly set audio and text trackId a", () => {
      const currentResult = parseLanguageTracks({
        textTracks: [textTrack],
        audioTracks: [audioTrack],
        selectedAudioTrack: 0,
        selectedTextTrack: 0,
      });

      expect(currentResult.audioTrackId).toBe(audioTrack.id);
      expect(currentResult.textTrackId).toBe(textTrack.id);
    });

    it("should return audio and subtitle track id as undefined if -1 values are passed", () => {
      const currentResult = parseLanguageTracks({
        textTracks: [textTrack],
        audioTracks: [audioTrack],
        selectedAudioTrack: -1,
        selectedTextTrack: -1,
      });

      expect(currentResult.audioTrackId).not.toBeDefined();
      expect(currentResult.textTrackId).not.toBeDefined();
    });
  });
});
