{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaladan.dev/contracts/theme/v1/common.schema.json",
  "title": "Theme contract — shared definitions",
  "description": "Definitions used by both theme.yaml and sections/*.yaml. Descriptions here are the documentation: they survive into generated editors, typed SDKs, and whatever a coding agent reads.",

  "$defs": {
    "LocalizedText": {
      "type": "object",
      "description": "A string per locale, keyed by BCP 47 tag. Every merchant-visible string in a theme is localized, because a theme published to the store is read by merchants in more than one language even when the shops it renders are not.",
      "propertyNames": { "pattern": "^[a-z]{2,3}(-[A-Za-z]{2,4})*$" },
      "additionalProperties": { "type": "string" },
      "minProperties": 1
    },

    "Handle": {
      "type": "string",
      "description": "Lowercase, hyphenated, stable. Never renamed once published.",
      "pattern": "^[a-z][a-z0-9]*(-[a-z0-9]+)*$",
      "maxLength": 60
    },

    "SectionRef": {
      "description": "One entry in a starting layout — a section id on its own, or an id with the preset it starts from. The bare form takes the section's first preset, which is what a theme that has never thought about presets wants. Naming one is how two styles put the same section on the page wearing different clothes.",
      "oneOf": [
        { "$ref": "#/$defs/Handle" },
        {
          "type": "object",
          "properties": {
            "section": { "$ref": "#/$defs/Handle" },
            "preset": {
              "$ref": "#/$defs/Handle",
              "description": "A preset's `handle` in that section's sections/<id>.yaml. A preset with no handle cannot be named here; the first one is still reachable by the bare form."
            }
          },
          "required": ["section"],
          "additionalProperties": false
        }
      ]
    },

    "SettingId": {
      "type": "string",
      "description": "Identifies a setting within its section or within the theme's globals. It is a key in the merchant's settings document, so renaming it discards every merchant's value for it — see manifest.md, 'Setting ids are a contract'.",
      "pattern": "^[a-zA-Z][a-zA-Z0-9_]*$",
      "maxLength": 60
    },

    "SettingType": {
      "description": "What the builder renders and what the theme receives. 'header' and 'paragraph' carry no value and exist only to structure a long settings panel.",
      "enum": [
        "text", "textarea", "richtext",
        "number", "range", "checkbox",
        "select", "radio", "color",
        "image", "video", "url", "font",
        "product", "collection", "category", "page", "blog", "article", "metaobject",
        "header", "paragraph"
      ]
    },

    "SettingOption": {
      "type": "object",
      "properties": {
        "value": { "type": "string" },
        "label": { "$ref": "#/$defs/LocalizedText" }
      },
      "required": ["value", "label"],
      "additionalProperties": false
    },

    "VisibleIf": {
      "type": "object",
      "description": "Hides a control in the editor when it is irrelevant. Editor-only: the value is not removed, and the theme still receives whatever was last set. A theme decides for itself whether to render it, usually by checking the same condition.",
      "properties": {
        "setting": { "$ref": "#/$defs/SettingId" },
        "equals": {},
        "in": { "type": "array", "minItems": 1 }
      },
      "required": ["setting"],
      "oneOf": [{ "required": ["equals"] }, { "required": ["in"] }],
      "additionalProperties": false
    },

    "Setting": {
      "type": "object",
      "description": "One control in the visual builder, and one key in the settings document.",
      "properties": {
        "id": { "$ref": "#/$defs/SettingId" },
        "type": { "$ref": "#/$defs/SettingType" },
        "label": { "$ref": "#/$defs/LocalizedText" },
        "info": {
          "$ref": "#/$defs/LocalizedText",
          "description": "A sentence of help beneath the control. Where a setting has a non-obvious consequence, this is cheaper than a support ticket."
        },
        "default": {
          "description": "Used when the merchant has never touched this setting. A theme MUST fall back to it: a shop whose owner has not opened the builder still has to render."
        },
        "placeholder": { "$ref": "#/$defs/LocalizedText" },
        "maxLength": { "type": "integer", "minimum": 1 },
        "min": { "type": "number" },
        "max": { "type": "number" },
        "step": { "type": "number", "exclusiveMinimum": 0 },
        "options": {
          "type": "array",
          "items": { "$ref": "#/$defs/SettingOption" },
          "minItems": 1
        },
        "visibleIf": { "$ref": "#/$defs/VisibleIf" }
      },
      "required": ["type"],
      "additionalProperties": false,
      "allOf": [
        {
          "if": { "properties": { "type": { "enum": ["header", "paragraph"] } }, "required": ["type"] },
          "then": { "required": ["label"] },
          "else": { "required": ["id", "label"] }
        },
        {
          "if": { "properties": { "type": { "enum": ["select", "radio"] } }, "required": ["type"] },
          "then": { "required": ["options"] }
        },
        {
          "if": { "properties": { "type": { "const": "range" } }, "required": ["type"] },
          "then": { "required": ["min", "max", "step"] }
        }
      ]
    },

    "SettingGroup": {
      "type": "object",
      "description": "A titled group of settings in the editor panel.",
      "properties": {
        "group": { "$ref": "#/$defs/LocalizedText" },
        "settings": {
          "type": "array",
          "items": { "$ref": "#/$defs/Setting" },
          "minItems": 1
        }
      },
      "required": ["group", "settings"],
      "additionalProperties": false
    }
  }
}
