{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openpkg.dev/schemas/v0.4.0/openpkg.schema.json",
  "title": "OpenPkg Specification v0.4.0",
  "description": "Minimal tool-agnostic TypeScript export representation",
  "type": "object",
  "required": ["openpkg", "meta", "exports"],
  "properties": {
    "$schema": {
      "type": "string",
      "description": "Reference to the OpenPkg schema version"
    },
    "openpkg": {
      "type": "string",
      "const": "0.4.0"
    },
    "meta": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "version": { "type": "string" },
        "description": { "type": "string" },
        "license": { "type": "string" },
        "repository": { "type": "string" },
        "ecosystem": { "type": "string" }
      }
    },
    "exports": {
      "type": "array",
      "items": { "$ref": "#/$defs/export" }
    },
    "types": {
      "type": "array",
      "items": { "$ref": "#/$defs/typeDef" }
    },
    "examples": {
      "type": "array",
      "items": { "$ref": "#/$defs/example" }
    },
    "extensions": {
      "type": "object",
      "properties": {
        "presentation": {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/presentationMeta" }
        }
      },
      "additionalProperties": true
    },
    "generation": {
      "$ref": "#/$defs/generationMeta",
      "description": "Optional generation metadata"
    }
  },
  "$defs": {
    "generationMeta": {
      "type": "object",
      "properties": {
        "generator": { "type": "string" },
        "timestamp": { "type": "string", "format": "date-time" }
      }
    },
    "presentationMeta": {
      "type": "object",
      "properties": {
        "slug": { "type": "string" },
        "displayName": { "type": "string" },
        "category": { "type": "string" },
        "importPath": { "type": "string" },
        "alias": { "type": "string" }
      },
      "additionalProperties": false
    },
    "export": {
      "type": "object",
      "required": ["id", "name", "kind"],
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "kind": {
          "type": "string",
          "enum": [
            "function",
            "class",
            "variable",
            "interface",
            "type",
            "enum",
            "module",
            "namespace",
            "reference",
            "external"
          ]
        },
        "description": { "type": "string" },
        "examples": {
          "type": "array",
          "items": { "$ref": "#/$defs/example" }
        },
        "signatures": {
          "type": "array",
          "items": { "$ref": "#/$defs/signature" }
        },
        "type": { "type": "string" },
        "schema": { "$ref": "#/$defs/schema" },
        "members": {
          "type": "array",
          "items": { "$ref": "#/$defs/member" }
        },
        "extends": { "type": "string" },
        "implements": {
          "type": "array",
          "items": { "type": "string" }
        },
        "tags": {
          "type": "array",
          "items": { "$ref": "#/$defs/tag" }
        },
        "source": { "$ref": "#/$defs/sourceLocation" },
        "deprecated": { "type": "boolean" },
        "flags": { "type": "object", "additionalProperties": true },
        "typeParameters": {
          "type": "array",
          "items": { "$ref": "#/$defs/typeParameter" }
        },
        "typeAliasKind": { "$ref": "#/$defs/typeAliasKind" },
        "conditionalType": { "$ref": "#/$defs/conditionalType" },
        "mappedType": { "$ref": "#/$defs/mappedType" },
        "decorators": {
          "type": "array",
          "items": { "$ref": "#/$defs/decorator" }
        }
      }
    },
    "typeDef": {
      "type": "object",
      "required": ["id", "name", "kind"],
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "kind": {
          "type": "string",
          "enum": ["interface", "type", "enum", "class", "external"]
        },
        "description": { "type": "string" },
        "schema": { "$ref": "#/$defs/schema" },
        "type": { "type": "string" },
        "members": {
          "type": "array",
          "items": { "$ref": "#/$defs/member" }
        },
        "extends": { "type": "string" },
        "implements": {
          "type": "array",
          "items": { "type": "string" }
        },
        "tags": {
          "type": "array",
          "items": { "$ref": "#/$defs/tag" }
        },
        "source": { "$ref": "#/$defs/sourceLocation" },
        "rawComments": { "type": "string" },
        "typeAliasKind": { "$ref": "#/$defs/typeAliasKind" },
        "conditionalType": { "$ref": "#/$defs/conditionalType" },
        "mappedType": { "$ref": "#/$defs/mappedType" }
      }
    },
    "tag": {
      "type": "object",
      "required": ["name", "text"],
      "properties": {
        "name": { "type": "string" },
        "text": { "type": "string" },
        "param": { "$ref": "#/$defs/tagParam" }
      },
      "additionalProperties": false
    },
    "tagParam": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "type": { "type": "string" },
        "description": { "type": "string" },
        "optional": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "signature": {
      "type": "object",
      "properties": {
        "parameters": {
          "type": "array",
          "items": { "$ref": "#/$defs/parameter" }
        },
        "returns": { "$ref": "#/$defs/returns" },
        "description": { "type": "string" },
        "typeParameters": {
          "type": "array",
          "items": { "$ref": "#/$defs/typeParameter" }
        },
        "overloadIndex": { "type": "integer", "minimum": 0 },
        "isImplementation": { "type": "boolean" },
        "throws": {
          "type": "array",
          "items": { "$ref": "#/$defs/throwsInfo" }
        }
      }
    },
    "typeParameter": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "constraint": { "type": "string" },
        "default": { "type": "string" }
      }
    },
    "parameter": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "required": { "type": "boolean" },
        "schema": { "$ref": "#/$defs/schema" },
        "description": { "type": "string" },
        "default": {},
        "rest": { "type": "boolean" },
        "decorators": {
          "type": "array",
          "items": { "$ref": "#/$defs/decorator" }
        }
      }
    },
    "returns": {
      "type": "object",
      "properties": {
        "schema": { "$ref": "#/$defs/schema" },
        "description": { "type": "string" }
      }
    },
    "schema": {
      "oneOf": [
        { "type": "string" },
        { "type": "boolean" },
        {
          "type": "object",
          "properties": {
            "$schema": { "type": "string" },
            "type": { "type": "string" },
            "format": { "type": "string" },
            "enum": { "type": "array" },
            "const": {},
            "items": { "$ref": "#/$defs/schema" },
            "prefixedItems": {
              "type": "array",
              "items": { "$ref": "#/$defs/schema" }
            },
            "properties": {
              "type": "object",
              "additionalProperties": { "$ref": "#/$defs/schema" }
            },
            "required": { "type": "array", "items": { "type": "string" } },
            "additionalProperties": {
              "oneOf": [{ "type": "boolean" }, { "type": "object" }]
            },
            "anyOf": { "type": "array", "items": { "$ref": "#/$defs/schema" } },
            "allOf": { "type": "array", "items": { "$ref": "#/$defs/schema" } },
            "oneOf": { "type": "array", "items": { "$ref": "#/$defs/schema" } },
            "$ref": { "type": "string" },
            "discriminator": {
              "type": "object",
              "properties": { "propertyName": { "type": "string" } }
            },
            "description": { "type": "string" },
            "deprecated": { "type": "boolean" },
            "x-deprecated-reason": { "type": "string" },
            "default": {},
            "minimum": { "type": "number" },
            "maximum": { "type": "number" },
            "exclusiveMinimum": { "type": "number" },
            "exclusiveMaximum": { "type": "number" },
            "minLength": { "type": "integer" },
            "maxLength": { "type": "integer" },
            "pattern": { "type": "string" },
            "minItems": { "type": "integer" },
            "maxItems": { "type": "integer" },
            "uniqueItems": { "type": "boolean" },
            "signatures": { "type": "array", "items": { "$ref": "#/$defs/signature" } }
          }
        }
      ]
    },
    "sourceLocation": {
      "type": "object",
      "properties": {
        "file": { "type": "string" },
        "line": { "type": "integer", "minimum": 1 },
        "url": { "type": "string" }
      }
    },
    "typeAliasKind": {
      "type": "string",
      "enum": ["alias", "conditional", "mapped", "template-literal", "infer"]
    },
    "conditionalType": {
      "type": "object",
      "required": ["checkType", "extendsType", "trueType", "falseType"],
      "properties": {
        "checkType": { "type": "string" },
        "extendsType": { "type": "string" },
        "trueType": { "type": "string" },
        "falseType": { "type": "string" }
      },
      "additionalProperties": false
    },
    "mappedType": {
      "type": "object",
      "required": ["typeParameter"],
      "properties": {
        "typeParameter": { "type": "string" },
        "nameType": { "type": "string" },
        "valueType": { "type": "string" },
        "readonly": {
          "oneOf": [{ "type": "boolean" }, { "enum": ["+", "-"] }]
        },
        "optional": {
          "oneOf": [{ "type": "boolean" }, { "enum": ["+", "-"] }]
        }
      },
      "additionalProperties": false
    },
    "decorator": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "arguments": { "type": "array" },
        "argumentsText": { "type": "array", "items": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "throwsInfo": {
      "type": "object",
      "properties": {
        "type": { "type": "string" },
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },
    "exampleLanguage": {
      "type": "string",
      "enum": ["ts", "js", "tsx", "jsx", "shell", "json"]
    },
    "example": {
      "oneOf": [
        { "type": "string" },
        {
          "type": "object",
          "required": ["code"],
          "properties": {
            "code": { "type": "string" },
            "title": { "type": "string" },
            "description": { "type": "string" },
            "language": { "$ref": "#/$defs/exampleLanguage" }
          },
          "additionalProperties": false
        }
      ]
    },
    "visibility": {
      "type": "string",
      "enum": ["public", "protected", "private"]
    },
    "member": {
      "type": "object",
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "kind": { "type": "string" },
        "description": { "type": "string" },
        "visibility": { "$ref": "#/$defs/visibility" },
        "tags": { "type": "array", "items": { "$ref": "#/$defs/tag" } },
        "flags": { "type": "object", "additionalProperties": true },
        "schema": { "$ref": "#/$defs/schema" },
        "signatures": { "type": "array", "items": { "$ref": "#/$defs/signature" } },
        "decorators": { "type": "array", "items": { "$ref": "#/$defs/decorator" } }
      }
    }
  }
}
