// import Vue from "vue";
// const crypto = require("crypto");
// import router from "@routes/router";
// import store from '../store/store';
// import exportFromJSON from "export-from-json";

// util to export to excel file using plugin
// https://github.com/zheeeng/export-from-json
// export const convertJsonToExcelUsingPlugin = async (
//   data,
//   fileName = "download"
// ) => {
//   // 'txt'(default), 'css', 'html', 'json', 'csv', 'xls', 'xml'
//   const exportType = exportFromJSON.types.xls;
//   const withBOM = true;

//   return await exportFromJSON({ data, fileName, exportType, withBOM });
// };

// util to export to excel file manually
export const exportJsonToExcelManually = (JSONData, FileTitle, ShowLabel) => {
  //If JSONData is not an object then JSON.parse will parse the JSON string in an Object
  var arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData;
  var CSV = "";
  //This condition will generate the Label/Header
  if (ShowLabel) {
    var row = "";
    //This loop will extract the label from 1st index of on array
    for (var index in arrData[0]) {
      //Now convert each value to string and comma-seprated
      row += encodeURI(index) + ",";
    }
    row = row.slice(0, -1);
    //append Label row with line break
    CSV += row + "\r\n";
  }
  //1st loop is to extract each row
  for (var i = 0; i < arrData.length; i++) {
    var row = "";
    //2nd loop will extract each column and convert it in string comma-seprated
    for (var index in arrData[i]) {
      row += '"' + arrData[i][index] + '",';
    }
    row.slice(0, row.length - 1);
    //add a line break after each row
    CSV += row + "\r\n";
  }
  if (CSV == "") {
    alert("Invalid data");
    return;
  }
  //Generate a file name
  // application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8;
  // text/csv;charset=utf-8;
  // var csvContent = "data:text/csv;charset=utf-8,%EF%BB%BF" + encodeURI(csvContent);

  var filename = FileTitle;
  var blob = new Blob([CSV], {
    type: "text/csv;charset=utf-8,BOM",
  });

  if (navigator.msSaveBlob) {
    // IE 10+
    navigator.msSaveBlob(blob, filename);
  } else {
    var link = document.createElement("a");
    if (link.download !== undefined) {
      // feature detection
      // Browsers that support HTML5 download attribute
      var url = URL.createObjectURL(blob);
      link.setAttribute("href", url);
      link.style = "visibility:hidden";
      link.download = filename + ".csv";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
    }
  }
};

// util to delay promise
export const wait = (ms) => {
  return (x) => {
    return new Promise((resolve) => setTimeout(() => resolve(x), ms));
  };
};
// util to createObjectByNewProperties
export const createObjectByNewProperties = (list, allowedProperties) => {
  const properties = [];

  list.forEach((item, index) => {
    const filtered = Object.keys(item)
      .filter((key) => allowedProperties.includes(key))
      .reduce((obj, key) => {
        obj[key] = item[key];
        return obj;
      }, {});

    properties[index] = filtered;
  });
  return properties;
};
// util to convert digits to character
export const toNumbersInCharacters = (number) => {
  const persianNumbers = ["۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹", "۰"];
  const arabicNumbers = ["١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩", "٠"];
  const englishNumbers = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
  const persianNumbersInCharacters = [
    "یک",
    "دو",
    "سه",
    "چهار",
    "پنج",
    "شش",
    "هفت",
    "هشت",
    "نه",
    "صفر",
  ];

  // only replace english number with persian number character
  return persianNumbersInCharacters[englishNumbers.indexOf(number)];

  // convert all numbers to persian digits
  // return str.split("").map(c =>
  //     persianNumbersInCharacters[englishNumbers.indexOf(c)] ||
  //     persianNumbersInCharacters[englishNumbers.indexOf(c)] || c).join("")
};
// util to handle if img src is not valid
export const handleImageSrcOnError = ({ target }, isUserAvatar = true) => {
  if (isUserAvatar) target.classList.add("human-avatar");
  target.classList.add("error");
};
// util to convert date to local date and time strng;
export const persianDateAndTime = (stringDate, local = "fa-IR") => {
  let date = new Date(stringDate);
  let jsonDate = {
    weekday: {
      long: date.toLocaleDateString(local, { weekday: "long" }),
      short: date.toLocaleDateString(local, { weekday: "short" }),
      narrow: date.toLocaleDateString(local, { weekday: "narrow" }),
    },
    era: {
      long: date.toLocaleDateString(local, { era: "long" }),
      short: date.toLocaleDateString(local, { era: "short" }),
      narrow: date.toLocaleDateString(local, { era: "narrow" }),
    },
    timeZoneName: {
      long: date.toLocaleDateString(local, { timeZoneName: "long" }),
      short: date.toLocaleDateString(local, { timeZoneName: "short" }),
    },
    year: {
      numeric: date.toLocaleDateString(local, { year: "numeric" }),
      "2-digit": date.toLocaleDateString(local, { year: "2-digit" }),
    },
    month: {
      numeric: date.toLocaleDateString(local, { month: "numeric" }),
      "2-digit": date.toLocaleDateString(local, { month: "2-digit" }),
      long: date.toLocaleDateString(local, { month: "long" }),
      short: date.toLocaleDateString(local, { month: "short" }),
      narrow: date.toLocaleDateString(local, { month: "narrow" }),
    },
    day: {
      numeric: date.toLocaleDateString(local, { day: "numeric" }),
      "2-digit": date.toLocaleDateString(local, { day: "2-digit" }),
    },
    hour: {
      numeric: date.toLocaleTimeString(local, { hour: "numeric" }),
      "2-digit": date.toLocaleTimeString(local, { hour: "2-digit" }),
    },
    minute: {
      numeric: date.toLocaleTimeString(local, { minute: "numeric" }),
      "2-digit": date.toLocaleTimeString(local, { minute: "2-digit" }),
    },
    second: {
      numeric: date.toLocaleTimeString(local, { second: "numeric" }),
      "2-digit": date.toLocaleTimeString(local, { second: "2-digit" }),
    },
  };
  return jsonDate;
};
// util to format numbers to local curency
export const formatNumber = (price = "") => {
  return new Intl.NumberFormat("fa-IR").format(price);
};

export const isValidHttpUrl = (string) => {
  let url;

  try {
    url = new URL(string);
  } catch (_) {
    return false;
  }

  return url.protocol === "http:" || url.protocol === "https:";
};
export const addJsCssFileToDom = (fileUrl, fileType, uuid) => {
  let targetElement =
    fileType == "js" ? "script" : fileType == "css" ? "link" : "none";
  let targetAttr =
    fileType == "js" ? "src" : fileType == "css" ? "href" : "none";

  let node = document.createElement(targetElement);
  node.setAttribute(targetAttr, fileUrl);
  node.setAttribute("id", targetElement + "-" + uuid);

  document.head.appendChild(node);
};
export const removeJsCssFileFromDom = (fileType, uuid) => {
  let targetElement =
    fileType == "js" ? "script" : fileType == "css" ? "link" : "none";
  document.getElementById(targetElement + "-" + uuid)?.remove();
};
export const clearBodyClass = (className = undefined) => {
  if (className) document.querySelector("body").replace(className, "");
  else document.querySelector("body").removeAttribute("class");
};
export const redirectToExternalLink = (href, openInNewTab) => {
  if (href) {
    if (isValidHttpUrl(href) && openInNewTab) window.open(href, "_blank");
    else if (isValidHttpUrl(href) && !openInNewTab) window.location.href = href;
    else if (!isValidHttpUrl(href) && openInNewTab) {
      if (href.includes("www.")) window.open("http://" + href, "_blank");
    } else if (!isValidHttpUrl(href) && !openInNewTab) {
      if (href.includes("www.")) window.location.href = "http://" + href;
    }
  }
};
export const extractAllQueryParams = (href) => {
  return new URLSearchParams(href);

  // return new Proxy(new URLSearchParams(href), {
  // get: (searchParams, prop) => searchParams.get(prop),
  // });
};
export const makeQueryParams = (url, searchParams) => {
  var query = new URLSearchParams();

  // (A) URL SEARCH PARAMS OBJECT TO QUICKLY BUILD QUERY STRING
  Object.keys(searchParams).forEach((key) =>
    query.append(key, searchParams[key])
  );

  // (B) CONVERT TO STRING, APPEND TO URL
  url += "?" + query.toString();
  return url;
};

// export const decryptData = (
//   data,
//   key = "fTjWnZr4u7x!A%D*G-KaNdRgUkXp3s6v",
//   method = "AES-256-CBC",
//   iv = "poaskq2234??@35."
// ) => {
//   let decipher = crypto.createDecipheriv(method, key, iv);
//   let decrypted = decipher.update(data, "base64", "utf8");
//   let dd = decrypted + decipher.final("utf8");
//   return JSON.parse(dd);
// };

// util to handle response errors

export const handleErrors = (error) => {
  return new Promise((resolve, reject) => {
    /* 
      401: The HTTP 401 Unauthorized response status code indicates that
       the client request has not been completed because it lacks
       valid authentication credentials for the requested resource.

      403: The HTTP 403 Forbidden response status code indicates that
      the server understands the request but refuses to authorize it.

      404:  The HTTP 404 Not Found response status code indicates that 
      the server cannot find the requested resource
      
      405: The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed
       response status code indicates that the server knows the
      request method, but the target resource doesn't support this method

      406: Not Acceptable
      */
    try {
      let res = error.response;

      if (res) {
        if (res.status === 401) {
          mySwalToast({
            title: "خطا!",
            text: res.data.message,
          });

          //   router.push({ name: 'login' })
        } else if (res.status === 403) {
          mySwalToast({
            title: "خطا!",
            text: res.data.message,
          });
        } else if (res.status === 404) {
          mySwalToast({
            title: "خطا!",
            text: res.data.message,
          });

          // router.push('/404')
        } else if (res.status === 405) {
          mySwalToast({
            title: "خطا!",
            text: res.data.message,
          });

          // router.push('/404')
        } else if (res.status === 406) {
          mySwalToast({
            title: "خطا!",
            text: res.data.message,
          });

          // router.push('/404')
        } else if (res.status === 500) {
          mySwalToast({
            title: "خطا!",
            text: res.data.message ?? res.data,
          });
        } else {
          mySwalToast({
            title: "خطا!",
            text: res.data.message,
          });
        }

        resolve(error.response);
      } else {
        console.info(error);
        let message = "خطا!!!";

        // if (error) {
        //   message = error.message ?? error;
        // }
        // else
        // {
        //   message = "خطا!!!"
        // }

        reject(new Error(message));

        // text: error.stack,
        mySwalToast({
          title: message,
        });
      }
    } catch (error) {
      let message = "خطا!!!";
      message = error?.message ?? message;

      return reject(new Error(message));
    }
  });
};

export const routeHandleErrors = (error, next) => {
  try {
    if (error) {
      if (error.status === 401) {
        mySwalToast({
          title: "خطا!",
          text: error.data.message,
        });

        if (isLoginRemoved) {
          store.dispatch("loginAsGuest2").then(() => next("/"));
        } else {
          store.dispatch("logout").then(() =>
            next({
              name: "login",
            })
          );
        }
      } else if (error.status === 403) {
        mySwalToast({
          title: "خطا!",
          text: error.data.message,
        });
      } else if (error.status === 404) {
        mySwalToast({
          title: "خطا!",
          text: error.data.message,
        });

        // router.push('/404')
      } else if (error.status === 405) {
        mySwalToast({
          title: "خطا!",
          text: error.data.message,
        });

        // router.push('/404')
      } else if (error.status === 406) {
        mySwalToast({
          title: "خطا!",
          text: error.data.message,
        });

        // router.push('/404')
      } else if (error.status === 500) {
        mySwalToast({
          title: "خطا!",
          text: error.data.message ?? error.data,
        });
      } else {
        mySwalToast({
          title: "خطا!",
          text: error.data.message,
        });
      }
    } else {
      console.info(error);
      let message = "خطا!!!";

      mySwalToast({
        title: message,
      });
    }
  } catch (exeption) {
    console.info(exeption);

    let message = "خطا!!!";
    message = exeption?.message ?? message;
  }
};
function mySwalToast(options) {
  Vue.swal({
    // width: '37em',
    imageWidth: "3em",
    imageHeight: "3em",
    showConfirmButton: false,

    icon: "error",
    toast: true,
    position: "bottom-end",
    timerProgressBar: true,
    timer: 5000,
    confirmButtonColor: "unset",
    cancelButtonColor: "unset",
    ...options,
  });
}
function mySwalConfirmModal(options) {
  Vue.swal({
    title: "خطا!",
    icon: "error",
    confirmButtonText: "بستن",
    ...options,
  });
}

function countDownTimer(timeInSecond = 120) {
  var timeleft = timeInSecond;
  var downloadTimer = setInterval(function () {
    if (timeleft <= 0) {
      clearInterval(downloadTimer);
      // document.getElementById("countdown").innerHTML = "Finished";
    } else {
      // document.getElementById("countdown").innerHTML =
      // timeleft + " seconds remaining";
    }
    timeleft -= 1;
  }, 1000);

  return timeleft;
}

///////////////////////////////////////
export function normalizeText(
  text,
  options = { charArNormal: true, errorLine: true }
) {
  // //defualt
  // options = {
  //   charArNormal: true,
  //   errorLine: true,
  //   alphabetsNormal: false,
  //   emptylinesNormal: false,
  //   hamzehNormal: false,
  //   spaceCorrection: false,
  //   trimLine: false
  // };

  // console.log(text)

  text = text.trim();

  //اصلاح خطاهای متن در کد خط
  if (options?.errorLine) {
    text = text.replaceAll("\\n", "\n");
    text = text.replaceAll("\\r", "\r");
    text = replaceRegText("(\r\n)+", "\n", text);
    text = text.replaceAll("\r", "\n");

    //شنبهها ==> شنبه‌ها
    //رسیدگیهای ==> رسیدگی‌های
    // text = replaceRegText("(ه)(ها)", /\1\2/gm, text);
  }
  // یکسان سازی اختلاف حروف کیبوردهای مختلف - کدهای مختلف ولی نمایش یکسان
  if (options?.alphabetsNormal) {
    text = replaceAlphabets(text);
  }

  //حذف خطوط و فاصله‌های خالی
  if (options?.emptylinesNormal) {
    text = replaceRegText("( )+", " ", text);
    text = replaceRegText("(\n)+", "\n", text);
  }

  // نرمال کردن حروف اختلافی عربی و فارسی
  if (options?.charArNormal) {
    text = text.replaceAll("ي", "ی");
    text = text.replaceAll("ك", "ک");
  }

  // پرش از اختلافات همزه‌ای
  if (options?.hamzehNormal) {
    text = replaceRegText("ئ|ﺋ", "ی", text);
    text = replaceRegText("ؤ|ﺅ", "و", text);
    text = replaceRegText("ﺔ|ۀ|ة", "ه", text);
    text = replaceRegText("إ|أ", "ا", text);
  }
  if (options?.dateNormal) {
    // فرمت YYYY/MM/DD یا YYYY-MM-DD
    text = replaceRegText(
      /(\d{4})[\/\-](\d{1,2})[\/\-](\d{1,2})/,
      (_, year, month, day) =>
        `${year}/${month.padStart(2, "0")}/${day.padStart(2, "0")}`,
      text
    );

    // فرمت DD/MM/YYYY یا DD-MM-YYYY
    text = replaceRegText(
      /(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,
      (_, day, month, year) =>
        `${year}/${month.padStart(2, "0")}/${day.padStart(2, "0")}`,
      text
    );

    // فرمت MM/DD/YYYY
    text = replaceRegText(
      /(\d{1,2})[\/\-](\d{1,2})[\/\-](\d{4})/,
      (_, month, day, year) =>
        `${year}/${month.padStart(2, "0")}/${day.padStart(2, "0")}`,
      text
    );
  }
  if (options?.spaceCorrection) {
    text = spaceCorrection(text);
  }

  // حذف خطوط اضافی اول و انتهای متن
  if (options?.trimLine) text = text.replace(/^\s+|\s+$/g, "");

  text = text.trim();

  return text;
}

export function replaceAlphabets(text) {
  let res = text;
  res = res.replaceAll("\u00AD", "\u200C");
  res = res.replaceAll("\u00AC", "\u200C"); // نیم اسپیس خاص

  res = replaceRegText("ﺁ|آ", "آ", res);
  res = replaceRegText("ﺎ|ٱ|ﺍ", "ا", res); //
  res = replaceRegText("ٲ|أ|ﺄ|ﺃ", "أ", res);
  res = replaceRegText("ﺈ|ﺇ", "إ", res);
  res = replaceRegText("ﺐ|ﺏ|ﺑ|ﺒ", "ب", res);
  res = replaceRegText("ﭖ|ﭗ|ﭙ|ﭘ", "پ", res);
  res = replaceRegText("ﭡ|ٺ|ٹ|ﭞ|ٿ|ټ|ﺕ|ﺗ|ﺖ|ﺘ", "ت", res);
  res = replaceRegText("ﺙ|ﺛ|ﺚ|ﺜ", "ث", res);
  res = replaceRegText("ﺝ|ڃ|ﺠ|ﺟ", "ج", res);
  res = replaceRegText("ڃ|ﭽ|ﭼ", "چ", res);
  res = replaceRegText("ﺢ|ﺤ|څ|ځ|ﺣ", "ح", res);
  res = replaceRegText("ﺥ|ﺦ|ﺨ|ﺧ", "خ", res);
  res = replaceRegText("ڏ|ډ|ﺪ|ﺩ", "د", res);
  res = replaceRegText("ﺫ|ﺬ|ذ", "ذ", res);
  res = replaceRegText("ڙ|ڗ|ڒ|ڑ|ڕ|ﺭ|ﺮ", "ر", res);
  res = replaceRegText("ﺰ|ﺯ", "ز", res);
  res = replaceRegText("ﮊ", "ژ", res);
  res = replaceRegText("ݭ|ݜ|ﺱ|ﺲ|ښ|ﺴ|ﺳ", "س", res);
  res = replaceRegText("ﺵ|ﺶ|ﺸ|ﺷ", "ش", res);
  res = replaceRegText("ﺺ|ﺼ|ﺻ|ﺹ", "ص", res); //
  res = replaceRegText("ﺽ|ﺾ|ﺿ|ﻀ", "ض", res);
  res = replaceRegText("ﻁ|ﻂ|ﻃ|ﻄ", "ط", res);
  res = replaceRegText("ﻆ|ﻇ|ﻈ", "ظ", res);
  res = replaceRegText("ڠ|ﻉ|ﻊ|ﻋ|ﻌ", "ع", res);
  res = replaceRegText("ﻎ|ۼ|ﻍ|ﻐ|ﻏ", "غ", res);
  res = replaceRegText("ﻒ|ﻑ|ﻔ|ﻓ", "ف", res);
  res = replaceRegText("ﻕ|ڤ|ﻖ|ﻗ|ﻘ", "ق", res);
  res = replaceRegText("ڭ|ﻚ|ﮎ|ﻜ|ﮏ|ګ|ﻛ|ﮑ|ﮐ|ڪ|ك", "ک", res);
  res = replaceRegText("ﮚ|ﮒ|ﮓ|ﮕ|ﮔ", "گ", res);
  res = replaceRegText("ﻝ|ﻞ|ﻠ|ڵ|ﻟ", "ل", res); //
  res = replaceRegText("ﻵ|ﻶ|ﻷ|ﻸ|ﻹ|ﻺ|ﻻ|ﻼ", "لا", res); //
  res = replaceRegText("ﻡ|ﻤ|ﻢ|ﻣ", "م", res);
  res = replaceRegText("ڼ|ﻦ|ﻥ|ﻨ|ﻧ", "ن", res);
  res = replaceRegText("ވ|ﯙ|ۈ|ۋ|ﺆ|ۊ|ۇ|ۏ|ۅ|ۉ|ﻭ|ﻮ|ؤ", "و", res);
  res = replaceRegText("ﻬ|ھ|ﻩ|ﻫ|ﻪ|ە|ہ", "ه", res);
  res = replaceRegText("ﭛ|ﻯ|ۍ|ﻰ|ﻱ|ﻲ|ں|ﻳ|ﻴ|ﯼ|ې|ﯽ|ﯾ|ﯿ|ێ|ے|ى|ي", "ی", res);
  res = replaceRegText("¬", "‌", res);
  res = replaceRegText("•|·|●|·|・|∙|｡|ⴰ", ".", res);
  res = replaceRegText(",|٬|٫|‚|،", "،", res);
  res = replaceRegText("ʕ", "؟", res);
  res = replaceRegText("۰|٠", "0", res);
  res = replaceRegText("۱|١", "1", res);
  res = replaceRegText("۲|٢", "2", res);
  res = replaceRegText("۳|٣", "3", res);
  res = replaceRegText("۴|٤", "4", res);
  res = replaceRegText("۵", "5", res);
  res = replaceRegText("۶|٦", "6", res);
  res = replaceRegText("۷|٧", "7", res);
  res = replaceRegText("۸|٨", "8", res);
  res = replaceRegText("۹|٩", "9", res);
  res = replaceRegText("²", "2", res);
  res = replaceRegText("|ِ|ُ|َ|ٍ|ٌ|ً|", "", res);
  // res = replaceRegText("ـ", "_", res);
  res = replaceRegText("ـ", "-", res); //
  return res;
}

export function spaceCorrection(text) {
  let res = text;
  res = replaceRegText("^(بی|می|نمی)( )", "$1‌", res);
  res = replaceRegText("( )(می|نمی|بی)( )", "$1$2‌", res);
  res = replaceRegText(
    "( )(هایی|ها|های|ایی|هایم|هایت|هایش|هایمان|هایتان|هایشان|ات|ان|ین|انی|بان|ام|ای|یم|ید|اید|اند|بودم|بودی|بود|بودیم|بودید|بودند|ست)( )",
    "‌$2$3",
    res
  );
  res = replaceRegText("( )(شده|نشده)( )", "‌$2‌", res);
  res = replaceRegText(
    "( )(طلبان|طلب|گرایی|گرایان|شناس|شناسی|گذاری|گذار|گذاران|شناسان|گیری|پذیری|بندی|آوری|سازی|بندی|کننده|کنندگان|گیری|پرداز|پردازی|پردازان|آمیز|سنجی|ریزی|داری|دهنده|آمیز|پذیری|پذیر|پذیران|گر|ریز|ریزی|رسانی|یاب|یابی|گانه|گانه‌ای|انگاری|گا|بند|رسانی|دهندگان|دار)( )",
    "‌$2$3",
    res
  );
  return res;
}

export function replaceRegText(pattern, replacement, text) {
  const regex = new RegExp(pattern, "g");
  return text.replace(regex, replacement);
}

//////////////////////////////////////////

// export { wait, handleErrors }
