import Enact_$L from "@enact/i18n/$L";

import stringReSource from "../../resources/de/strings.json";

export const $L = (str) => {
  let languageSetting = "system";
  let language = "en";

  if (
    typeof window === "object" &&
    window.store &&
    window.store.getState().common &&
    window.store.getState().common.localSettings
  ) {
    languageSetting =
      window.store.getState().common.localSettings.languageSetting;
  }

  if (
    typeof window === "object" &&
    window.PalmSystem &&
    languageSetting === "system"
  ) {
    return Enact_$L(str).replace(/{br}/g, "{br}");
  } else if (typeof window === "object") {
    language =
      typeof window.navigator === "object"
        ? window.navigator.language || window.navigator.userLanguage
        : "en-US";

    if (languageSetting !== "system") {
      language = languageSetting;
    }

    language = language.split("-")[0];

    const resource = stringReSource[language] || stringReSource.en;

    if (typeof str === "object") {
      if (resource && resource[str.key]) {
        return resource[str.key].replace(/{br}/g, "{br}");
      } else {
        return str.value;
      }
    } else if (resource && resource[str]) {
      return resource[str].replace(/{br}/g, "{br}");
    }
  }

  return str && str.replace(/{br}/g, "{br}");
};

export const createQueryString = (object) => {
  const parts = [];
  for (const key of Object.getOwnPropertyNames(object)) {
    if (
      object[key] !== null &&
      object[key] !== undefined &&
      object[key] !== ""
    ) {
      parts.push(`${key}=${encodeURIComponent(object[key])}`);
    }
  }
  return parts.join("&");
};

export const wait = (time) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
};

export const scaleW = (value) => {
  if (typeof window === "object") {
    return value * (window.innerWidth / 1920);
  }
  return value;
};

export const scaleH = (value) => {
  if (typeof window === "object") {
    return value * (window.innerHeight / 1080);
  }
  return value;
};

//for test


let localLaunchParams = {
  // contentTarget: "V2_aaa_dummy_OS_categorycode_subcategorycode",
};
export const getLaunchParams = () => {
  let params = {};

  if (
    typeof window === "object" &&
    window.PalmSystem &&
    window.PalmSystem.launchParams
  ) {
    try {
      params = JSON.parse(window.PalmSystem.launchParams);
      if (params["x-webos-app-container-launch"] === true) {
        params = params.details;
      }
    } catch (e) {
      params = {};
    }
    return params;
  } else {
    return localLaunchParams;
  }
};

export const clearLaunchParams = () => {
	console.log('common.clearLaunchParams');
	if (typeof window === 'object' && window.PalmSystem && window.PalmSystem.launchParams) {
		window.PalmSystem.launchParams = '';
	}else{
		localLaunchParams = {};
	}
};

export const readLocalStorage = (key, defaultValue) => {
  const value = typeof window === 'object' && window.localStorage.getItem(key);
  if (!value && defaultValue !== undefined) {
    return defaultValue;
  }
  return value === 'undefined' ? null : JSON.parse(value);
};

export const writeLocalStorage = (key, value) => {
  if (typeof window === 'object') {
    window.localStorage.setItem(key, JSON.stringify(value));
  }
};