import { Utilities } from '@vue-leaflet/vue-leaflet';
import type { PropType } from 'vue';
import type { IRouter, LineOptions } from 'leaflet-routing-machine';
import type * as L from 'leaflet';

export interface RoutingControlProps {
  waypoints: unknown[];
  router?: IRouter | undefined;
  plan?: unknown;
  fitSelectedRoutes?: string | boolean;
  lineOptions?: LineOptions | undefined;
  routeLine?: ((route: unknown) => unknown) | undefined;
  createMarker?:
    | ((i: number, waypoint: { latLng: L.LatLng }, n: number) => L.Marker | false)
    | undefined;
  autoRoute?: boolean;
  routeWhileDragging?: boolean;
  routeDragInterval?: number;
  waypointMode?: string;
  useZoomParameter?: boolean;
  showAlternatives?: boolean;
  altLineOptions?: LineOptions | undefined;
}

export const routingControlProps = {
  waypoints: {
    type: Array as PropType<unknown[]>,
    default: () => [],
  },
  router: {
    type: Object as PropType<IRouter | undefined>,
    default: undefined,
  },
  plan: {
    type: Object as PropType<unknown>,
    default: undefined,
  },
  fitSelectedRoutes: {
    type: [String, Boolean] as PropType<string | boolean>,
    default: 'smart',
  },
  lineOptions: {
    type: Object as PropType<LineOptions | undefined>,
    default: undefined,
  },
  routeLine: {
    type: Function as PropType<((route: unknown) => unknown) | undefined>,
    default: undefined,
  },
  createMarker: {
    type: Function as PropType<
      ((i: number, waypoint: { latLng: L.LatLng }, n: number) => L.Marker | false) | undefined
    >,
    default: undefined,
  },
  autoRoute: {
    type: Boolean,
    default: true,
  },
  routeWhileDragging: {
    type: Boolean,
    default: false,
  },
  routeDragInterval: {
    type: Number,
    default: 500,
  },
  waypointMode: {
    type: String,
    default: 'connect',
  },
  useZoomParameter: {
    type: Boolean,
    default: false,
  },
  showAlternatives: {
    type: Boolean,
    default: false,
  },
  altLineOptions: {
    type: Object as PropType<LineOptions | undefined>,
    default: undefined,
  },
};

export const setupRoutingControl = (props: RoutingControlProps) => {
  const options = Utilities.propsToLeafletOptions(props, routingControlProps);

  return {
    options,
    methods: {} as Record<string, never>,
  };
};
