<script>
import {authenticatedRequest} from "@/components/session/authenticatedRequest.vue";
/**
 * The plates object
 */
export const plates = {
  get_order_history: async (plate) => {
    return authenticatedRequest(
        "/department/license-plate/lookup?plate=" + plate,
        "GET")
        .then((response) => {
          return response.data.data;
        })
        .catch((error) => {
          console.error(error);
        });
  },
  search_customer_plates: async (customer_number, plate, column = 'reg_1') => {
    return authenticatedRequest(
        "/department/license-plate/customer-lookup?customer_number=" + parseInt(customer_number) + "&plate=" + plate + "&column=" + column,
        "GET")
        .then((response) => {
          return response.data.data;
        })
        .catch((error) => {
          console.error(error);
        });
  },
  /**
   * Parse the motorapi string
   * This is used to decode the Unicode characters like u00f8 to ø
   * @param string The string to parse
   * @returns {*}
   */
  parse_motorapi_string: (string) => {
    {
      // Check if the string contains u00, if so add a backslash
      if (string.includes('u00')) {
        string = string.replace(/u00/g, '\\u00');
      }
      // unicode decode
      return string.replace(/\\u[\dA-F]{4}/gi,
          (match) => String.fromCharCode(parseInt(match.replace(/\\u/g, ''), 16)));
    }
  }
}
</script>