// @flow

import type { LinkInfo } from "../../components/forms/LinkInfo.jsx";
import type { FileInfo } from "../common/FileInfo.jsx";
import type { LocationInfo } from "../common/location/LocationInfo";
import { getLocationDisplayString } from "../common/location/LocationInfo";
import type { ProjectAPIData } from "./ProjectAPIUtils.js";
import type { Dictionary } from "../types/Generics.jsx";
import type { APIError, TagDefinition } from "./ProjectAPIUtils";

export type GroupDetailsAPIData = {|
  group_id: string,
  group_creator: string,
  group_name: string,
  group_description: string,
  group_short_description: string,
  group_location: ?string,
  group_country: ?string,
  group_state: ?string,
  group_city: ?string,
  group_url: ?string,
  group_date_modified: string,
  group_thumbnail: FileInfo,
  group_links: $ReadOnlyArray<LinkInfo>,
  group_files: $ReadOnlyArray<FileInfo>,
|};

export type ProjectRelationshipAPIData = {|
  project_relationship_id: number,
  relationship_is_approved: boolean,
|} & ProjectAPIData;

export type GroupTileAPIData = {|
  group_id: string,
  group_name: string,
  group_date_modified: string,
  group_location: ?string,
  group_country: ?string,
  group_state: ?string,
  group_city: ?string,
  group_thumbnail: FileInfo,
  group_project_count: number,
  group_issue_areas: Dictionary<number>,
|};

export default class GroupAPIUtils {
  static fetchGroupDetails(
    id: number,
    callback: GroupDetailsAPIData => void,
    errCallback: APIError => void
  ): void {
    fetch(new Request("/api/group/" + id + "/", { credentials: "include" }))
      .then(response => {
        if (!response.ok) {
          throw Error();
        }
        return response.json();
      })
      .then(groupDetails => callback(groupDetails))
      // TODO: Get catch to return http status code
      .catch(
        response =>
          errCallback &&
          errCallback({
            errorCode: response.status,
            errorMessage: JSON.stringify(response),
          })
      );
  }

  static fetchAllTags(
    callback: ($ReadOnlyArray<TagDefinition>) => void,
    errCallback: APIError => void
  ): Promise<$ReadOnlyArray<TagDefinition>> {
    return fetch(new Request("/api/tags/groups"))
      .then(response => response.json())
      .then(tags => callback(tags))
      .catch(
        response =>
          errCallback &&
          errCallback({
            errorCode: response.status,
            errorMessage: JSON.stringify(response),
          })
      );
  }

  static getLocationDisplayName(group: GroupDetailsAPIData): string {
    const location: LocationInfo = {
      location_id: group.group_location,
      city: group.group_city,
      state: group.group_state,
      country: group.group_country,
    };
    return getLocationDisplayString(location);
  }
}

@@ -41,22 +41,8 @@
 %22../
-../components/
 form
@@ -125,55 +125,12 @@
 ort 
-type %7B LocationInfo %7D from %22../common/location/
+%7B%0A  
 Loca
@@ -137,27 +137,19 @@
 tionInfo
-%22;%0Aimport %7B
+,%0A 
  getLoca
@@ -165,17 +165,18 @@
 ayString
- 
+,%0A
 %7D from %22
@@ -206,16 +206,19 @@
 tionInfo
+.js
 %22;%0Aimpor
@@ -225,16 +225,18 @@
 t type %7B
+%0A 
  Project
@@ -242,17 +242,47 @@
 tAPIData
- 
+,%0A  TagDefinition,%0A  APIError,%0A
 %7D from %22
@@ -364,74 +364,8 @@
 sx%22;
-%0Aimport type %7B APIError, TagDefinition %7D from %22./ProjectAPIUtils%22;
 %0A%0Aex
@@ -375,31 +375,28 @@
 t type Group
-Details
+Tile
 APIData = %7B%7C
@@ -428,15 +428,12 @@
 oup_
-creator
+name
 : st
@@ -438,36 +438,45 @@
 string,%0A  group_
-name
+date_modified
 : string,%0A  grou
@@ -477,29 +477,27 @@
   group_
-descrip
+loca
 tion: 
+?
 string,%0A
@@ -508,27 +508,18 @@
 oup_
-short_description
+country
 : 
+?
 stri
@@ -526,32 +526,29 @@
 ng,%0A  group_
-location
+state
 : ?string,%0A 
@@ -547,37 +547,34 @@
 tring,%0A  group_c
-ountr
+it
 y: ?string,%0A  gr
@@ -581,46 +581,149 @@
 oup_
-state: ?string,%0A  group_city: ?string,
+thumbnail: FileInfo,%0A  group_project_count: number,%0A  group_issue_areas: Dictionary%3CTagDefinition%3E,%0A%7C%7D;%0A%0Aexport type GroupDetailsAPIData = %7B%7C
 %0A  g
@@ -731,14 +731,17 @@
 oup_
-url
+creator
 : 
-?
 stri
@@ -749,36 +749,34 @@
 g,%0A  group_d
-ate_modified
+escription
 : string,%0A  
@@ -781,35 +781,41 @@
   group_
-thumbnail: FileInfo
+short_description: string
 ,%0A  grou
@@ -888,24 +888,43 @@
 ileInfo%3E,%0A%7C%7D
+ & GroupTileAPIData
 ;%0A%0Aexport ty
@@ -1056,321 +1056,8 @@
 a;%0A%0A
-export type GroupTileAPIData = %7B%7C%0A  group_id: string,%0A  group_name: string,%0A  group_date_modified: string,%0A  group_location: ?string,%0A  group_country: ?string,%0A  group_state: ?string,%0A  group_city: ?string,%0A  group_thumbnail: FileInfo,%0A  group_project_count: number,%0A  group_issue_areas: Dictionary%3Cnumber%3E,%0A%7C%7D;%0A%0A
 expo

Update Group Flow definitions