{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/kubohiroya/turbowarp-extension-manifest/schemas/extension-manifest.schema.json",
  "oneOf": [
    {
      "$ref": "#/$defs/manifestV1"
    },
    {
      "$ref": "#/$defs/manifestV2"
    }
  ],
  "$defs": {
    "manifestV1": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "formatVersion",
        "id",
        "blocks",
        "menus"
      ],
      "properties": {
        "formatVersion": {
          "const": 1
        },
        "id": {
          "$ref": "#/$defs/extensionId"
        },
        "blocks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/blockV1"
          }
        },
        "menus": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/menu"
          }
        }
      }
    },
    "manifestV2": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "formatVersion",
        "id",
        "blocks",
        "menus"
      ],
      "properties": {
        "formatVersion": {
          "const": 2
        },
        "id": {
          "$ref": "#/$defs/extensionId"
        },
        "blocks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/blockV2"
          }
        },
        "menus": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/menu"
          }
        },
        "pathSegmentType": {
          "$ref": "#/$defs/pathSegmentType"
        },
        "dataReferenceType": {
          "$ref": "#/$defs/dataReferenceType"
        }
      }
    },
    "extensionId": {
      "type": "string",
      "pattern": "^[a-z0-9]+$"
    },
    "argumentV1": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "type"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "type": "string",
          "minLength": 1
        },
        "menu": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "argumentV2": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "type"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "type": {
          "type": "string",
          "minLength": 1
        },
        "menu": {
          "type": "string",
          "minLength": 1
        },
        "normalizesTo": {
          "const": "pathSegments"
        },
        "staticLiteral": {
          "type": "boolean"
        },
        "minimum": {
          "type": "number"
        },
        "maximum": {
          "type": "number"
        }
      }
    },
    "blockV1": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "opcode",
        "blockType",
        "arguments"
      ],
      "properties": {
        "opcode": {
          "type": "string",
          "minLength": 1
        },
        "blockType": {
          "type": "string",
          "minLength": 1
        },
        "arguments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/argumentV1"
          }
        }
      }
    },
    "blockV2": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "opcode",
        "blockType",
        "arguments",
        "resultType",
        "effect",
        "immutable",
        "errors",
        "server"
      ],
      "properties": {
        "opcode": {
          "type": "string",
          "minLength": 1
        },
        "blockType": {
          "type": "string",
          "minLength": 1
        },
        "arguments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/argumentV2"
          }
        },
        "resultType": {
          "enum": [
            "json",
            "boolean",
            "number",
            "string",
            "void",
            "jsonText",
            "yamlText"
          ]
        },
        "effect": {
          "enum": [
            "pure",
            "immutable",
            "control",
            "request-read",
            "response-write",
            "storage-read",
            "storage-write",
            "binary-read",
            "binary-write",
            "state"
          ]
        },
        "immutable": {
          "type": "boolean"
        },
        "errors": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "server": {
          "$ref": "#/$defs/server"
        }
      }
    },
    "server": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "supported"
      ],
      "properties": {
        "supported": {
          "type": "boolean"
        },
        "irOperation": {
          "type": "string",
          "minLength": 1
        }
      },
      "if": {
        "properties": {
          "supported": {
            "const": true
          }
        }
      },
      "then": {
        "required": [
          "supported",
          "irOperation"
        ]
      }
    },
    "menu": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "acceptReporters"
      ],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "acceptReporters": {
          "type": "boolean"
        }
      }
    },
    "pathSegmentType": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "variants"
      ],
      "properties": {
        "kind": {
          "const": "discriminatedUnion"
        },
        "variants": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": [
              "kind",
              "valueType"
            ],
            "properties": {
              "kind": {
                "type": "string",
                "minLength": 1
              },
              "valueType": {
                "type": "string",
                "minLength": 1
              }
            }
          }
        }
      }
    },
    "dataReferenceType": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "kind",
        "scope",
        "lifetime",
        "valueType"
      ],
      "properties": {
        "kind": {
          "type": "string",
          "minLength": 1
        },
        "scope": {
          "type": "string",
          "minLength": 1
        },
        "lifetime": {
          "type": "string",
          "minLength": 1
        },
        "valueType": {
          "type": "string",
          "minLength": 1
        }
      }
    }
  }
}
