type PermissionKey<M extends string, S extends string, C extends string> = `${M}:${S}:${C}`;

type NestedPermissionsRecord<M extends string, T extends Record<string, readonly string[]>> = {
  readonly [S in keyof T & string]: {
    readonly [C in T[S][number]]: PermissionKey<M, S, C>;
  };
};

interface PermissionResult<M extends string, T extends Record<string, readonly string[]>> {
  permissions: NestedPermissionsRecord<M, T>;
}

export function definePermissions<
  const M extends string,
  const T extends Record<string, readonly string[]>,
>(moduleName: M, scopes: T): PermissionResult<M, T> {
  const permissions: Record<string, Record<string, string>> = {};

  for (const [scope, commands] of Object.entries(scopes)) {
    permissions[scope] = {};
    for (const cmd of commands) {
      permissions[scope][cmd] = `${moduleName}:${scope}:${cmd}`;
    }
  }

  return { permissions } as unknown as PermissionResult<M, T>;
}
