import { routeRefType } from './core-plugin-api/src/routing/types.esm.js';
import { createRouteRef, createSubRouteRef, createExternalRouteRef } from '@backstage/frontend-plugin-api';
import { toInternalRouteRef } from './frontend-plugin-api/src/routing/RouteRef.esm.js';
import { toInternalSubRouteRef } from './frontend-plugin-api/src/routing/SubRouteRef.esm.js';
import { toInternalExternalRouteRef } from './frontend-plugin-api/src/routing/ExternalRouteRef.esm.js';

function convertLegacyRouteRefs(refs) {
  return Object.fromEntries(
    Object.entries(refs).map(([name, ref]) => [
      name,
      convertLegacyRouteRef(ref)
    ])
  );
}
function convertLegacyRouteRef(ref) {
  const isNew = "$$type" in ref;
  const oldType = ref[routeRefType];
  if (isNew && oldType) {
    return ref;
  }
  if (isNew) {
    return convertNewToOld(
      ref
    );
  }
  return convertOldToNew(ref, oldType);
}
function convertNewToOld(ref) {
  if (ref.$$type === "@backstage/RouteRef") {
    const newRef = toInternalRouteRef(ref);
    return Object.assign(ref, {
      [routeRefType]: "absolute",
      params: newRef.getParams(),
      title: newRef.getDescription()
    });
  }
  if (ref.$$type === "@backstage/SubRouteRef") {
    const newRef = toInternalSubRouteRef(ref);
    return Object.assign(ref, {
      [routeRefType]: "sub",
      parent: convertLegacyRouteRef(newRef.getParent()),
      params: newRef.getParams()
    });
  }
  if (ref.$$type === "@backstage/ExternalRouteRef") {
    const newRef = toInternalExternalRouteRef(ref);
    return Object.assign(ref, {
      [routeRefType]: "external",
      optional: true,
      params: newRef.getParams(),
      defaultTarget: newRef.getDefaultTarget()
    });
  }
  throw new Error(
    `Failed to convert route ref, unknown type '${ref.$$type}'`
  );
}
function convertOldToNew(ref, type) {
  if (type === "absolute") {
    const legacyRef = ref;
    const legacyRefStr = String(legacyRef);
    const newRef = toInternalRouteRef(
      createRouteRef({
        params: legacyRef.params
      })
    );
    return Object.assign(legacyRef, {
      $$type: "@backstage/RouteRef",
      version: "v1",
      T: newRef.T,
      getParams() {
        return newRef.getParams();
      },
      getDescription() {
        return legacyRefStr;
      },
      setId(id) {
        newRef.setId(id);
      },
      toString() {
        return legacyRefStr;
      }
    });
  }
  if (type === "sub") {
    const legacyRef = ref;
    const legacyRefStr = String(legacyRef);
    const newRef = toInternalSubRouteRef(
      createSubRouteRef({
        path: legacyRef.path,
        parent: convertLegacyRouteRef(legacyRef.parent)
      })
    );
    return Object.assign(legacyRef, {
      $$type: "@backstage/SubRouteRef",
      version: "v1",
      T: newRef.T,
      getParams() {
        return newRef.getParams();
      },
      getParent() {
        return newRef.getParent();
      },
      getDescription() {
        return legacyRefStr;
      },
      toString() {
        return legacyRefStr;
      }
    });
  }
  if (type === "external") {
    const legacyRef = ref;
    const legacyRefStr = String(legacyRef);
    const newRef = toInternalExternalRouteRef(
      createExternalRouteRef({
        params: legacyRef.params,
        defaultTarget: "getDefaultTarget" in legacyRef ? legacyRef.getDefaultTarget() : void 0
      })
    );
    return Object.assign(legacyRef, {
      $$type: "@backstage/ExternalRouteRef",
      version: "v1",
      T: newRef.T,
      getParams() {
        return newRef.getParams();
      },
      getDescription() {
        return legacyRefStr;
      },
      // This might already be implemented in the legacy ref, but we override it just to be sure
      getDefaultTarget() {
        return newRef.getDefaultTarget();
      },
      setId(id) {
        newRef.setId(id);
      },
      toString() {
        return legacyRefStr;
      }
    });
  }
  throw new Error(`Failed to convert legacy route ref, unknown type '${type}'`);
}

export { convertLegacyRouteRef, convertLegacyRouteRefs };
//# sourceMappingURL=convertLegacyRouteRef.esm.js.map
