{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://tisserand.app/data/schema.json",
  "title": "Tisserand map (carte.json, versions 4 and 5)",
  "description": "Link map (entities, links, views) stored in a .tisserand file (ZIP archive). The order of the \"proprietes\" and \"positions\" objects, and the order of arrays, is significant. Tisserand ignores unknown keys. Keys and enumerated values are French words and are part of the format.",
  "type": "object",
  "required": ["format", "version", "titre", "types_entites", "types_liens", "entites", "liens", "vues"],
  "properties": {
    "format": {"const": "tisserand"},
    "version": {"type": "integer", "minimum": 4, "description": "1 to 3: former Markdown format; 4: JSON; 5: same structure, link types that declare \"Montant\" (amount) have been revised (financial relationship)"},
    "titre": {"type": "string"},
    "source": {"type": "string", "description": "Name of the original file (e.g. an .acbs)"},
    "types_entites": {"type": "array", "items": {"$ref": "#/$defs/type_entite"}},
    "types_liens": {"type": "array", "items": {"$ref": "#/$defs/type_lien"}},
    "entites": {"type": "array", "items": {"$ref": "#/$defs/entite"}},
    "liens": {"type": "array", "items": {"$ref": "#/$defs/lien"}},
    "vues": {"type": "array", "items": {"$ref": "#/$defs/vue"}},
    "exhibit": {"type": "object", "properties": {"titre": {"type": "string"}, "introduction": {"type": "string"}},
                "description": "Living Exhibit presentation (ADR-0035); optional"},
    "vue_active": {"type": "integer", "minimum": 0, "description": "Index in \"vues\" of the view displayed when the map was saved: the map reopens on it (ADR-0033); optional, absent = 0"},
    "images": {"type": "array", "items": {"$ref": "#/$defs/image"}}
  },
  "$defs": {
    "textes": {"type": "array", "items": {"type": "string"}},
    "proprietes": {
      "type": "object", "additionalProperties": {"type": "string"},
      "description": "Property name → value (text). Every element has at least \"Nom\" (name) and \"Memo\"."
    },
    "libelle": {
      "type": "array", "items": {"type": "string"},
      "description": "Parts of the label: \"{Property}\" (replaced by its value) or literal text (\"\\n\", \"-\"…)"
    },
    "point": {"type": "array", "items": {"type": "number"}, "minItems": 2, "maxItems": 2},
    "direction": {"enum": ["non orienté", "orienté", "double sens"]},
    "type_entite": {
      "type": "object", "required": ["nom"],
      "properties": {
        "nom": {"type": "string"},
        "icone": {"type": "string", "description": "Built-in icon (person, company…) or \"image:<name>\""},
        "parent": {"type": "string"},
        "libelle_seul": {"type": "boolean"},
        "proprietes": {"$ref": "#/$defs/textes"},
        "libelle": {"$ref": "#/$defs/libelle"}
      }
    },
    "type_lien": {
      "type": "object", "required": ["nom"],
      "properties": {
        "nom": {"type": "string"},
        "couleur": {"type": "string", "pattern": "^#[0-9a-fA-F]{6}$",
                    "description": "#000000 = certain; #ff0000 = hypothetical"},
        "direction": {"$ref": "#/$defs/direction"},
        "style": {"enum": ["plein", "tirets", "pointillés"]},
        "epaisseur": {"type": "number"},
        "proprietes": {"$ref": "#/$defs/textes"},
        "libelle": {"$ref": "#/$defs/libelle"},
        "teinte": {"type": "string", "pattern": "^#[0-9a-fA-F]{6}$",
                   "description": "optional colour the links of the type are drawn in; display only, \"couleur\" keeps the kind (certain or hypothetical)"}
      }
    },
    "entite": {
      "type": "object", "required": ["id", "type"],
      "properties": {
        "id": {"type": "string", "minLength": 1, "description": "Stable, unique identifier (e1, e2…)"},
        "type": {"type": "string", "description": "Name of an element of \"types_entites\""},
        "icone": {"type": "string", "description": "Empty = icon of the type"},
        "fichiers": {"$ref": "#/$defs/textes", "description": "Names of the attached files, in carte.fichiers/"},
        "liens_web": {"$ref": "#/$defs/textes"},
        "proprietes": {"$ref": "#/$defs/proprietes"}
      }
    },
    "lien": {
      "type": "object", "required": ["id", "type", "de", "vers"],
      "properties": {
        "id": {"type": "string", "minLength": 1, "description": "Stable, unique identifier (l1, l2…)"},
        "type": {"type": "string", "description": "Name of an element of \"types_liens\""},
        "de": {"type": "string", "description": "Identifier of the source entity"},
        "vers": {"type": "string", "description": "Identifier of the target entity"},
        "direction": {"anyOf": [{"const": ""}, {"$ref": "#/$defs/direction"}], "description": "Empty = that of the type"},
        "couleur": {"type": "string", "description": "Empty = that of the type"},
        "fichiers": {"$ref": "#/$defs/textes"},
        "liens_web": {"$ref": "#/$defs/textes"},
        "proprietes": {"$ref": "#/$defs/proprietes"}
      }
    },
    "vue": {
      "type": "object", "required": ["nom"],
      "properties": {
        "nom": {"type": "string"},
        "echelle": {"type": "number"},
        "origine": {"$ref": "#/$defs/point"},
        "positions": {"type": "object", "additionalProperties": {"$ref": "#/$defs/point"},
                      "description": "Entity identifier → [x, y] (centre of the icon). An entity is in the view if and only if it is listed here."},
        "liens": {"$ref": "#/$defs/textes", "description": "Identifiers of the links displayed in the view"},
        "description": {"type": "string", "description": "Text displayed with the view in a Living Exhibit (ADR-0035); optional"},
        "notes": {"type": "array", "items": {"$ref": "#/$defs/note"},
                  "description": "Analyst notes (ADR-0034): yellow rectangles of formatted text that explain the view; optional"},
        "courbes": {"type": "object", "additionalProperties": {"$ref": "#/$defs/point"},
                    "description": "Links bent by hand (ADR-0032): displayed link identifier → [t, h]; the curve passes through the point at fraction t of the source → target segment, offset by h times its length; optional"}
      }
    },
    "note": {
      "type": "object", "required": ["id", "position", "taille", "texte"],
      "properties": {
        "id": {"type": "string", "description": "Unique within the view (n1, n2…)"},
        "position": {"$ref": "#/$defs/point", "description": "Top-left corner"},
        "taille": {"$ref": "#/$defs/point", "description": "[width, height], from 80 × 40 to 5000"},
        "texte": {"type": "array", "items": {
          "type": "object", "required": ["texte"],
          "properties": {
            "texte": {"type": "string"},
            "gras": {"type": "boolean"},
            "italique": {"type": "boolean"},
            "couleur": {"type": "string", "pattern": "^#[0-9a-fA-F]{6}$", "description": "Text colour"},
            "surligne": {"type": "string", "pattern": "^#[0-9a-fA-F]{6}$", "description": "Highlight colour"}
          }}, "description": "Runs of text with the same style, in order"}
      }
    },
    "image": {
      "type": "object", "required": ["nom"],
      "properties": {
        "nom": {"type": "string", "description": "Image key; \"photo:<id>\" for a photo pasted on an entity"},
        "fichier": {"type": "string", "description": "Relative path in the archive (carte.images/…, or Nom.jpg for a photo)"},
        "donnees": {"type": "string", "contentEncoding": "base64"}
      },
      "oneOf": [{"required": ["fichier"]}, {"required": ["donnees"]}]
    }
  }
}
