// interface ColorClasses {
//     green?: string;
//     red?: string;
//     orange?: string;
// }
import parse from "html-react-parser";

export default class StringHelper {
  static isEmpty = (string) =>
    typeof string === "string" && string.length === 0;

  static boldSearchTerm = (text, term) => {
    if (text === undefined) {
      return "";
    }
    if (term === undefined || term === "") {
      return text;
    }

    const startPos = text.toLowerCase().indexOf(term.toLowerCase());

    if (startPos === -1) {
      return text;
    } else {
      const endPos = startPos + term.length;

      let toReturn =
        text.substring(0, startPos) +
        "<span style='font-weight: 800; color: #121212'>" +
        text.substring(startPos, endPos) +
        "</span>" +
        text.substring(endPos, text.length);

      return parse(toReturn);
    }
  };

  static ucfirst = (string) => {
    if (string && string.length > 0) {
      return string.charAt(0).toUpperCase() + string.slice(1);
    }
    return string;
  };

  static createAutocompleteName(item) {
    if (item.wikipedia !== "" && item.wikipedia !== undefined) {
      const wikiName = item.wikipedia.split(":");
      if (wikiName[0] === "pl") {
        return wikiName[1];
      }
    }

    if (item.type === "city") {
      return (
        item.name +
        (item.name !== item.name_suffix ? ", " + item.name_suffix : "")
      );
    } else if (item.type === "state_district") {
      return item.name;
    } else if (item.type === "service") {
      return (
        item.name +
        (item.city !== "" ? ", " + item.city : ", " + item.name_suffix)
      );
    } else if (item.type === "hamlet") {
      return (
        item.name +
        (item.name !== item.name_suffix ? ", " + item.name_suffix : "")
      );
    } else if (item.type === "village") {
      return (
        item.name +
        (item.name !== item.name_suffix ? ", " + item.name_suffix : "")
      );
    } else if (item.type === "locality") {
      return (
        item.name +
        (item.name !== item.name_suffix ? ", " + item.name_suffix : "")
      );
    } else if (item.type === "water") {
      return (
        item.name +
        (item.name !== item.name_suffix ? ", " + item.name_suffix : "")
      );
    } else if (item.type === "living_street") {
      return item.name + (item.city !== "" ? ", " + item.city : "");
    } else if (item.type === "neighbourhood") {
      return item.name + (item.city !== "" ? ", " + item.city : "");
    } else if (item.type === "residential") {
      return item.name + (item.city !== "" ? ", " + item.city : "");
    } else if (item.type === "secondary") {
      return item.name + (item.city !== "" ? ", " + item.city : "");
    } else if (item.type === "suburb") {
      return item.name + (item.city !== "" ? ", " + item.city : "");
    } else if (item.type === "city_district") {
      return item.name + (item.city !== "" ? ", " + item.city : "");
    } else if (item.type === "courier_point") {
      return (
        item.name +
        (item.city !== "" ? ", " + item.city : "") +
        (item.street !== "" ? ", " + item.street : "")
      );
    } else if (item.type === "postcode") {
      return (
        item.name +
        (item.name !== item.name_suffix && item.name_suffix !== ""
          ? ", " + item.name_suffix
          : "")
      );
    } else if (item.type === "administrative") {
      return (
        item.name +
        (item.name !== item.name_suffix && item.name_suffix !== ""
          ? ", " + item.name_suffix
          : "")
      );
    } else if (item.type === "house") {
      return (
        "ul." +
        item.street +
        (item.building_number ? " " + item.building_number : "") +
        ", " +
        item.name +
        (item.name !== item.name_suffix ? ", " + item.name_suffix : "")
      );
    }
    return (
      item.name +
      (item.name !== item.name_suffix ? ", " + item.name_suffix : "")
    );
  }

  static preparePointDescription(point) {
    let toReturn = "";
    toReturn +=
      point.name +
      ", " /*.replaceAll(' ', '&nbsp;').replaceAll('-', '&#8209;')*/;
    // if (
    //   point.point_id != null &&
    //   point.point_id != '' &&
    //   point.point_id != point.name
    // ) {
    //   toReturn += ' ' + point.point_id + ',';
    // }
    /*toReturn += ', ';*/
    toReturn += point.address.street;
    if (
      point.address.building_number != null &&
      point.address.building_number !== ""
    ) {
      toReturn += " " + point.address.building_number;
      if (
        point.address.locale_number != null &&
        point.address.locale_number !== ""
      ) {
        toReturn += "/" + point.address.locale_number;
      }
    }

    if (point.address.postal_code || point.address.city) {
      toReturn += ", ";
      if (point.address.postal_code) {
        toReturn += point.address.postal_code;
      }
      if (point.address.postal_code && point.address.city) {
        toReturn += ", ";
      }
      if (point.address.city) {
        toReturn += point.address.city;
      }
    }
    return toReturn;
  }

  static preparePointDescriptionToPopup(point) {
    let toReturn = `<div style="display: flex; flex-direction: column;">`;
    if (point.name) {
      toReturn +=
        '<div style="text-wrap: wrap;">' +
        point.name.replaceAll(" ", "&nbsp;").replaceAll("-", "&#8209;") +
        "</div>";
    }
    // toReturn += ',<br>';
    toReturn += '<div style="text-wrap: wrap;">';
    if (point.address) {
      if (point.address.street) {
        toReturn += point.address.street
          .replaceAll(" ", "&nbsp;")
          .replaceAll("-", "&#8209;");
      }
      if (
        point.address.building_number != null &&
        point.address.building_number !== ""
      ) {
        toReturn +=
          "&nbsp;" + point.address.building_number.replaceAll("-", "&#8209;");

        if (
          point.address.locale_number != null &&
          point.address.locale_number !== ""
        ) {
          toReturn +=
            "/" + point.address.locale_number.replaceAll("-", "&#8209;");
        }
      }
    }

    // toReturn += ',<br></div>';
    toReturn += ',</div><div style="text-wrap: wrap;">';
    if (point.address.postal_code) {
      toReturn +=
        point.address.postal_code.replaceAll("-", "&#8209;") + "&nbsp;";
    }
    if (point.address.city) {
      toReturn +=
        point.address.city
          .replaceAll(" ", "&nbsp;")
          .replaceAll("-", "&#8209;") + ",&nbsp;";
    }
    if (point.address.country) {
      toReturn += point.address.country
        .replaceAll(" ", "&nbsp;")
        .replaceAll("-", "&#8209;");
    }

    toReturn += "</div></div>";
    // toReturn +=
    //   point.address.postal_code.replaceAll('-', '&#8209;') +
    //   '&nbsp;' +
    //   point.address.city.replaceAll(' ', '&nbsp;').replaceAll('-', '&#8209;') +
    //   ',&nbsp;' +
    //   point.address.country
    //     .replaceAll(' ', '&nbsp;')
    //     .replaceAll('-', '&#8209;');
    return toReturn;
  }

  static buildOpenedHours(hours) {
    if (hours.open_from !== null && hours.open_from !== "") {
      return (
        "<span>\
            " +
        hours.open_from +
        " - " +
        hours.open_to +
        "\
          </span>"
      );
    } else {
      return "<span>nieczynne</span>";
    }
  }

  static buildTodayOpen(hours, colors = {}) {
    const now = new Date();
    const timezoneOptions = { hour12: false, timeZone: "Europe/Warsaw" };
    const hour = parseInt(
      now.toLocaleString("pl-PL", {
        ...{ hour: "numeric" },
        ...timezoneOptions,
      }),
    );
    const minute = parseInt(
      now.toLocaleString("pl-PL", {
        ...{ minute: "numeric" },
        ...timezoneOptions,
      }),
    );
    const timestamp = hour * 60 + minute;

    const startHour = parseInt(
      !!hours.open_from ? hours.open_from.split(":")[0] : "0",
    );
    const startMinute = parseInt(
      !!hours.open_from ? hours.open_from.split(":")[1] : "0",
    );
    const startTimestamp = startHour * 60 + startMinute;

    const endHour = parseInt(
      !!hours.open_to ? hours.open_to.split(":")[0] : "0",
    );
    const endMinute = parseInt(
      hours.open_to ? hours.open_to.split(":")[1] : "0",
    );
    const endTimestamp = endHour * 60 + endMinute;

    if (!hours.open_from && !hours.open_to) {
      return (
        "<div class='" +
        (!!colors.red ? colors.red : "") +
        "'>Dzisiaj zamknięte.</div>"
      );
    } else if (timestamp >= startTimestamp) {
      if (timestamp <= endTimestamp) {
        if (timestamp >= endTimestamp - 30) {
          //zaraz zamknięte
          return (
            "<div class='" +
            (!!colors.orange ? colors.orange : "") +
            "'>Dzisiaj otwarte do " +
            hours.open_to +
            ".<br />\
                  " +
            (endTimestamp - timestamp) +
            " minut do zamknięcia.</div>"
          );
        } else {
          //otwarte
          return (
            "<div class='" +
            (!!colors.green ? colors.green : "") +
            "'>Dzisiaj otwarte do " +
            hours.open_to +
            ".</div>"
          );
        }
      } else {
        //już zamknięte
        return (
          "<div class='" +
          (!!colors.red ? colors.red : "") +
          "'>Dzisiaj zamknięte</div>"
        );
      }
    } else {
      //otwarte od
      return (
        "<div class='" +
        (!!colors.orange ? colors.orange : "") +
        "'>Otwarte od " +
        hours.open_from +
        ".</div>"
      );
    }
  }
}
