{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "SquizFormattedTextSchema.json",
  "type": "array",
  "title": "FormattedText",
  "items": {
    "$ref": "#/definitions/FormattedNodes"
  },

  "definitions": {
    "FormattedNodes": {
      "anyOf": [{ "$ref": "#/definitions/HigherOrderFormattedNodes" }, { "$ref": "#/definitions/BaseFormattedNodes" }]
    },

    "HigherOrderFormattedNodes": {
      "anyOf": [
        { "$ref": "#/definitions/FormattedTextLinkToMatrixAsset" },
        { "$ref": "#/definitions/FormattedTextLinkToDamAsset" },
        { "$ref": "#/definitions/FormattedTextMatrixImage" },
        { "$ref": "#/definitions/FormattedTextDamImage" }
      ]
    },

    "BaseFormattedNodes": {
      "anyOf": [
        { "$ref": "#/definitions/FormattedTextTag" },
        { "$ref": "#/definitions/FormattedTextFragment" },
        { "$ref": "#/definitions/TextNode" },
        { "$ref": "#/definitions/ComponentNode" },
        { "$ref": "#/definitions/LayoutNode" }
      ]
    },

    "TextNode": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "type": { "const": "text" },
        "value": { "type": "string" }
      },
      "required": ["type", "value"]
    },

    "LayoutNode": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "type": { "const": "layout" },
        "layout": {
          "type": "object",
          "properties": {
            "name": { "type": "string" },
            "options": {
              "type": "object",
              "additionalProperties": true
            }
          },
          "required": ["name", "options"]
        },
        "content": {
          "type": "object",
          "additionalProperties": true
        }
      },
      "required": ["type", "layout", "content"]
    },

    "FormattingOptions": {
      "type": "object",

      "properties": {
        "alignment": {
          "type": "string",
          "enum": ["center", "left", "right", "justify"]
        }
      }
    },

    "WithChildrenNode": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/FormattedNodes"
      }
    },

    "FormattedNodeFontProperties": {
      "type": "object",

      "properties": {
        "bold": { "type": "boolean" },
        "underline": { "type": "boolean" },
        "italics": { "type": "boolean" },
        "color": { "type": "string" },
        "size": { "type": "string" },
        "fontFamily": { "type": "string" }
      }
    },

    "FormattedTextTag": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "children": {
          "$ref": "#/definitions/WithChildrenNode"
        },
        "formattingOptions": { "$ref": "#/definitions/FormattingOptions" },
        "type": { "const": "tag" },
        "tag": { "type": "string", "minLength": 1 },
        "attributes": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        },
        "font": { "$ref": "#/definitions/FormattedNodeFontProperties" }
      },
      "required": ["type", "tag"]
    },

    "FormattedTextFragment": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "children": {
          "$ref": "#/definitions/WithChildrenNode"
        },
        "i18n": {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "type": "object",
            "properties": {
              "children": {
                "$ref": "#/definitions/WithChildrenNode"
              }
            },
            "additionalProperties": false,
            "required": []
          },
          "required": []
        },
        "type": { "const": "fragment" }
      },
      "required": ["type"]
    },

    "ComponentNode": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "type": { "const": "component" },
        "componentId": {
          "type": "string"
        },
        "componentType": {
          "type": "string",
          "enum": ["edge", "server"]
        },
        "i18n": {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "type": "object",
            "properties": {
              "id": { "type": "string" }
            },
            "required": []
          },
          "required": []
        },
        "componentSet": { "type": "string" },
        "contentItem": {
          "type": "object",
          "title": "ContentItemReference",
          "properties": {
            "id": { "type": "string" }
          },
          "required": []
        },
        "error": {
          "type": "string",
          "required": []
        }
      },
      "required": ["type", "componentSet", "componentId", "componentType"]
    },

    "FormattedTextLinkToMatrixAsset": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "children": {
          "$ref": "#/definitions/WithChildrenNode"
        },
        "formattingOptions": { "$ref": "#/definitions/FormattingOptions" },
        "type": { "const": "link-to-matrix-asset" },
        "matrixIdentifier": { "type": "string", "minLength": 1 },
        "matrixDomain": { "type": "string", "minLength": 1 },
        "matrixAssetId": { "type": "string", "minLength": 1 },
        "target": { "type": "string", "enum": ["_blank", "_self", "_parent", "_top"] },
        "attributes": {
          "allOf": [
            {
              "type": "object",
              "properties": {
                "href": false,
                "target": false
              }
            },
            {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            }
          ]
        },
        "error": {
          "type": "string",
          "required": []
        }
      },
      "required": ["type", "matrixDomain", "matrixAssetId", "target", "children"]
    },

    "FormattedTextLinkToDamAsset": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "children": {
          "$ref": "#/definitions/WithChildrenNode"
        },
        "formattingOptions": { "$ref": "#/definitions/FormattingOptions" },
        "type": { "const": "link-to-dam-asset" },
        "damSystemType": { "type": "string", "minLength": 1 },
        "damSystemIdentifier": { "type": "string", "minLength": 1 },
        "damObjectId": { "type": "string", "minLength": 1 },
        "damAdditional": { "type": "object", "properties": { "variant": { "type": "string", "minLength": 1 } } },
        "target": { "type": "string", "enum": ["_blank", "_self", "_parent", "_top"] },
        "attributes": {
          "allOf": [
            {
              "type": "object",
              "properties": {
                "href": false,
                "target": false
              }
            },
            {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            }
          ]
        }
      },
      "required": ["type", "damSystemType", "damSystemIdentifier", "damObjectId", "target", "children"]
    },

    "FormattedTextMatrixImage": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "type": { "const": "matrix-image" },
        "matrixIdentifier": { "type": "string", "minLength": 1 },
        "matrixDomain": { "type": "string", "minLength": 1 },
        "matrixAssetId": { "type": "string", "minLength": 1 },
        "formattingOptions": { "$ref": "#/definitions/FormattingOptions" },
        "attributes": {
          "allOf": [
            {
              "type": "object",
              "properties": {
                "src": false
              }
            },
            {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            }
          ]
        },
        "error": {
          "type": "string",
          "required": []
        }
      },
      "required": ["type", "matrixDomain", "matrixAssetId"]
    },

    "FormattedTextDamImage": {
      "type": "object",
      "additionalProperties": false,

      "properties": {
        "type": { "const": "dam-image" },
        "damSystemType": { "type": "string", "minLength": 1 },
        "damSystemIdentifier": { "type": "string", "minLength": 1 },
        "damObjectId": { "type": "string", "minLength": 1 },
        "damAdditional": { "type": "object", "properties": { "variant": { "type": "string", "minLength": 1 } } },
        "formattingOptions": { "$ref": "#/definitions/FormattingOptions" },
        "attributes": {
          "allOf": [
            {
              "type": "object",
              "properties": {
                "src": false
              }
            },
            {
              "type": "object",
              "additionalProperties": {
                "type": "string"
              }
            }
          ]
        }
      },
      "required": ["type", "damSystemType", "damSystemIdentifier", "damObjectId"]
    }
  }
}
