{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://nextgram.dev/schemas/plugin-v1.json",
  "title": "NextGram plugin manifest",
  "description": "Manifest for a NextGram .ngp plugin, format version 1.",
  "type": "object",
  "required": [
    "format",
    "formatVersion",
    "id",
    "name",
    "version",
    "versionCode",
    "kind",
    "api",
    "platforms",
    "description",
    "author",
    "license",
    "icon",
    "permissions"
  ],
  "additionalProperties": true,
  "properties": {
    "format": {
      "const": "ngp"
    },
    "formatVersion": {
      "const": 1
    },
    "id": {
      "$ref": "#/$defs/pluginId"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 64
    },
    "version": {
      "$ref": "#/$defs/semver"
    },
    "versionCode": {
      "type": "integer",
      "minimum": 1
    },
    "kind": {
      "enum": ["code", "script", "java", "ts", "hybrid"],
      "description": "code/java: DEX; script/ts: JavaScript; hybrid: both with script as the primary entry point."
    },
    "api": {
      "const": 1
    },
    "minHost": {
      "$ref": "#/$defs/versionRange"
    },
    "maxHost": {
      "$ref": "#/$defs/versionRange"
    },
    "platforms": {
      "type": "array",
      "minItems": 1,
      "uniqueItems": true,
      "items": {
        "enum": ["android", "desktop", "*"]
      }
    },
    "description": {
      "type": "string",
      "minLength": 1,
      "maxLength": 2000
    },
    "author": {
      "type": "object",
      "required": ["name"],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        },
        "telegram": {
          "type": "string",
          "pattern": "^@?[A-Za-z0-9_]{3,64}$"
        },
        "url": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "license": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "homepage": {
      "type": "string",
      "format": "uri"
    },
    "source": {
      "type": "string",
      "format": "uri"
    },
    "support": {
      "type": "string",
      "format": "uri"
    },
    "changelog": {
      "$ref": "#/$defs/safePath"
    },
    "icon": {
      "$ref": "#/$defs/imagePath"
    },
    "screenshots": {
      "type": "array",
      "maxItems": 6,
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/imagePath"
      }
    },
    "permissions": {
      "type": "array",
      "uniqueItems": true,
      "maxItems": 64,
      "items": {
        "$ref": "#/$defs/permission"
      }
    },
    "dependencies": {
      "type": "array",
      "maxItems": 32,
      "items": {
        "type": "object",
        "required": ["id", "version"],
        "additionalProperties": false,
        "properties": {
          "id": {
            "$ref": "#/$defs/pluginId"
          },
          "version": {
            "$ref": "#/$defs/versionRange"
          },
          "optional": {
            "type": "boolean",
            "default": false
          }
        }
      }
    },
    "conflicts": {
      "type": "array",
      "uniqueItems": true,
      "maxItems": 32,
      "items": {
        "$ref": "#/$defs/pluginId"
      }
    },
    "provides": {
      "type": "array",
      "uniqueItems": true,
      "maxItems": 32,
      "items": {
        "$ref": "#/$defs/serviceKey"
      }
    },
    "update": {
      "type": "object",
      "required": ["url"],
      "additionalProperties": false,
      "properties": {
        "url": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    "localization": {
      "type": "object",
      "maxProperties": 20,
      "propertyNames": {
        "pattern": "^[a-z]{2,3}(-[A-Za-z0-9]{2,8})*$"
      },
      "additionalProperties": {
        "type": "object",
        "minProperties": 1,
        "additionalProperties": false,
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64
          },
          "description": {
            "type": "string",
            "minLength": 1,
            "maxLength": 2000
          }
        }
      }
    },
    "experimental": {
      "type": "boolean",
      "default": false
    },
    "deprecated": {
      "type": "boolean",
      "default": false
    },
    "entrypoints": {
      "type": "object",
      "minProperties": 1,
      "additionalProperties": false,
      "properties": {
        "android": {
          "$ref": "#/$defs/javaClass"
        },
        "desktop": {
          "$ref": "#/$defs/javaClass"
        }
      }
    },
    "script": {
      "const": "script/main.js"
    },
    "nativeMethods": {
      "type": "array",
      "uniqueItems": true,
      "maxItems": 64,
      "items": {
        "type": "string",
        "pattern": "^[a-zA-Z][a-zA-Z0-9_]{0,63}$"
      }
    },
    "resources": {
      "type": "array",
      "uniqueItems": true,
      "maxItems": 8,
      "items": {
        "enum": ["font", "sound", "wallpaper", "icon", "strings", "haptics", "motion", "chrome"]
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "kind": {
            "const": "code"
          }
        },
        "required": ["kind"]
      },
      "then": {
        "required": ["entrypoints"],
        "properties": {
          "script": false,
          "platforms": {
            "type": "array",
            "items": {
              "enum": ["android", "desktop"]
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "java"
          }
        },
        "required": ["kind"]
      },
      "then": {
        "required": ["entrypoints"],
        "properties": {
          "script": false,
          "platforms": {
            "type": "array",
            "items": {
              "enum": ["android", "desktop"]
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "script"
          }
        },
        "required": ["kind"]
      },
      "then": {
        "required": ["script", "minHost"],
        "properties": {
          "entrypoints": false
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "ts"
          }
        },
        "required": ["kind"]
      },
      "then": {
        "required": ["script", "minHost"],
        "properties": {
          "entrypoints": false
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "const": "hybrid"
          }
        },
        "required": ["kind"]
      },
      "then": {
        "required": ["script", "entrypoints", "minHost"],
        "properties": {
          "platforms": {
            "type": "array",
            "contains": {
              "const": "android"
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "kind": {
            "enum": ["hybrid", "script", "ts"]
          }
        },
        "required": ["kind"]
      },
      "else": {
        "properties": {
          "nativeMethods": false
        }
      }
    },
    {
      "if": {
        "properties": {
          "platforms": {
            "type": "array",
            "contains": {
              "const": "*"
            }
          }
        },
        "required": ["platforms"]
      },
      "then": {
        "properties": {
          "platforms": {
            "type": "array",
            "maxItems": 1
          }
        }
      }
    }
  ],
  "$defs": {
    "pluginId": {
      "type": "string",
      "minLength": 3,
      "maxLength": 128,
      "pattern": "^[a-z][a-z0-9]*(\\.[a-z0-9][a-z0-9_-]*)+$"
    },
    "serviceKey": {
      "type": "string",
      "minLength": 3,
      "maxLength": 128,
      "pattern": "^[a-z][a-z0-9]*(\\.[a-z][a-z0-9_-]*)+$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-((?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+([0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$"
    },
    "versionRange": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "safePath": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._/-]*$"
    },
    "imagePath": {
      "type": "string",
      "minLength": 12,
      "maxLength": 256,
      "pattern": "^assets/[A-Za-z0-9][A-Za-z0-9._/-]*\\.(png|webp)$"
    },
    "javaClass": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "^[A-Za-z_$][A-Za-z0-9_$]*(\\.[A-Za-z_$][A-Za-z0-9_$]*)*$"
    },
    "permission": {
      "enum": [
        "ui.settings",
        "ui.appearance",
        "ui.chat",
        "ui.chatlist",
        "ui.profile",
        "ui.drawer",
        "ui.composer",
        "ui.screens",
        "ui.custom_screens",
        "ui.resources",
        "ui.strings",
        "messages.read",
        "messages.send",
        "messages.modify",
        "messages.background",
        "messages.react",
        "messages.automation",
        "chats.read",
        "chats.members.read",
        "chats.create",
        "chats.join",
        "chats.admin",
        "internal.access",
        "users.read",
        "contacts.read",
        "media.read",
        "media.write",
        "notifications.read",
        "notifications.manage",
        "accounts.list",
        "accounts.switch",
        "telegram.actions",
        "network",
        "clipboard.read",
        "clipboard.write",
        "location",
        "background"
      ]
    }
  }
}
