import { Location, StringCoordinates, StringLocation } from "./types";

export const splitLocation = (stringifiedLocation: StringLocation) => {
  return stringifiedLocation.replace(' ', '~').split('~');
};

export const stringifyLocation = (location: Location): StringLocation => {
  const { coordinates, address } = location;
  const [latitude, longitude] = coordinates;
  const coordinatesString = `${latitude},${longitude}` satisfies StringCoordinates;
  return `${coordinatesString} ${address}`;
};

const locationRe = new RegExp(/^[-0-9]+.[0-9]+,[-0-9]+.[0-9]+ .*/);
export const isValidLocation = (value: string): value is StringLocation => locationRe.test(value);