import * as URI from "uri-js";

import type { Track } from "@applets/core/types.d.ts";
import { SCHEME } from "./constants";
import { fetchHandles, fetchHandlesList, recursiveList, trackHandleId } from "./common";
import { expose } from "@scripts/common";

////////////////////////////////////////////
// ACTIONS
////////////////////////////////////////////
const actions = expose({
  consult,
  contextualize,
  list,
  resolve,
});

export type Actions = typeof actions;

// Actions

export async function consult(fileUriOrScheme: string) {
  if (!self.FileSystemDirectoryHandle) {
    return { supported: false, reason: "File System Access API is not supported" };
  }

  if (!fileUriOrScheme.includes(":")) {
    if (fileUriOrScheme !== SCHEME) return { supported: false, reason: "Scheme does not match" };
    return { supported: true };
  }

  const handles = await fetchHandles();
  const uri = URI.parse(fileUriOrScheme);
  if (uri.scheme !== SCHEME) return { supported: false, reason: "Scheme does not match" };
  return { supported: true, consultation: uri.host && !!handles[uri.host] };
}

export async function contextualize(cachedTracks: Track[]) {}

export async function list(cachedTracks: Track[] = []) {
  const handles = await fetchHandlesList();

  // Recursive listing of all tracks of available handles
  const processed: Track[][] = await Promise.all(
    handles.map(({ id, handle }) => {
      return recursiveList(handle, id, []);
    }),
  );

  // Group tracks by handle id & index by track uri
  const cache = cachedTracks.reduce((acc: Record<string, Record<string, Track>>, track: Track) => {
    const handleId = trackHandleId(track);
    if (!handleId) return acc;

    return { ...acc, [handleId]: { ...(acc[handleId] || {}), [track.uri]: track } };
  }, {});

  // Replace indexes in groups of which we have the handle.
  // Keeping around tracks with handles we don't have access to,
  // and removing tracks that are no longer available (for handles we do have access to).
  const groups = processed.flat(1).reduce(
    (acc, track) => {
      const handleId = trackHandleId(track);
      if (!handleId) throw new Error("New tracks are missing a handle id!");

      return { ...acc, [handleId]: { ...acc[handleId], [track.uri]: track } };
    },
    handles.reduce((acc: Record<string, Record<string, Track>>, handle) => {
      return { ...acc, [handle.id]: {} };
    }, cache),
  );

  // Transform in track list and sort by uri
  const data = Object.values(groups)
    .map((tracks) => Object.values(tracks))
    .flat(1)
    .sort((a: any, b: any) => {
      if (a.uri < b.uri) return -1;
      if (a.uri > b.uri) return 1;
      return 0;
    });

  // Fin
  return data;
}

export async function resolve(args: { uri: string }) {
  const fileUri = args.uri;

  const uri = URI.parse(fileUri);
  if (uri.scheme !== SCHEME) return undefined;
  if (!uri.host || !uri.path) return undefined;

  const handles = await fetchHandles();
  const handle = handles[uri.host];
  if (!handle) return undefined;

  const path = URI.unescapeComponent(uri.path);
  const parts = (path.startsWith("/") ? path.slice(1) : path).split("/");
  const filename = parts[parts.length - 1];

  const dirHandle = await parts
    .slice(0, -1)
    .reduce(
      async (
        acc: Promise<FileSystemDirectoryHandle>,
        part: string,
      ): Promise<FileSystemDirectoryHandle> => {
        const h = await acc;
        return await h.getDirectoryHandle(part);
      },
      Promise.resolve(handle),
    );

  const fileHandle = await dirHandle.getFileHandle(filename);
  const file = await fileHandle.getFile();
  const url = URL.createObjectURL(file);

  return { expiresAt: Infinity, url };
}
