/**
 * TYPE_DEFS — arg definitions & property definitions for each primitive type.
 * Used by the property editor panel.
 *
 * Each entry:
 *   args: [{l: label, s: step, t: 'text'|undefined}]
 *   props: [{k: key, l: label, t: type, s: step, o: options}]
 *   defaults: [default arg values]
 *   cat: category key
 *   desc: short description
 *   icon: emoji icon
 */

const TYPE_CATEGORIES = [
  { key: 'shapes', label: 'Formes', icon: '◆' },
  { key: 'arcs', label: 'Arcs & Anneaux', icon: '◠' },
  { key: 'text', label: 'Texte', icon: 'T' },
  { key: 'ticks', label: 'Graduations', icon: '|||' },
  { key: 'progress', label: 'Progression', icon: '▶' },
  { key: 'indicators', label: 'Indicateurs', icon: '↗' },
  { key: 'zones', label: 'Zones', icon: '◐' },
  { key: 'logic', label: 'Logique', icon: '⚡' }
];

/** Available custom fonts (DSL key → CSS font-family). */
const FONT_OPTIONS = ['sans-serif','orbitron','russo','teko','rajdhani','mono','audiowide'];

/** Common blink prop appended to every element type. */
const _BLINK = {k:'blink',l:'Clignotement (ms)',t:'number',s:50};

const TYPE_DEFS = {
  circle: {
    cat: 'shapes', desc: 'Cercle plein ou contour', icon: '○',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:1}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'stroke',l:'Contour',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'alpha',l:'Opacite',t:'number',s:0.1},{k:'glow',l:'Lueur',t:'number',s:1},_BLINK],
    defaults: ['50','50','40']
  },
  rect: {
    cat: 'shapes', desc: 'Rectangle avec coins arrondis', icon: '▭',
    args: [{l:'x',s:1},{l:'y',s:1},{l:'largeur',s:1},{l:'hauteur',s:1}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'stroke',l:'Contour',t:'color'},{k:'sw',l:'Epaisseur',t:'number',s:0.1},{k:'corner',l:'Coins',t:'number',s:0.5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['20','20','60','60']
  },
  line: {
    cat: 'shapes', desc: 'Ligne entre deux points', icon: '╱',
    args: [{l:'x1',s:1},{l:'y1',s:1},{l:'x2',s:1},{l:'y2',s:1}],
    props: [{k:'stroke',l:'Couleur',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'cap',l:'Terminaison',t:'select',o:['round','butt','square']},{k:'dash',l:'Tirets',t:'text'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['20','50','80','50']
  },
  polygon: {
    cat: 'shapes', desc: 'Polygone regulier (3+ cotes)', icon: '⬡',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:1},{l:'cotes',s:1}],
    props: [{k:'rotation',l:'Rotation (deg)',t:'number',s:1},{k:'fill',l:'Remplissage',t:'color'},{k:'stroke',l:'Contour',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','30','6']
  },
  shape: {
    cat: 'shapes', desc: 'Forme libre (sommets x y ...)', icon: '⬠',
    args: [{l:'x1',s:1},{l:'y1',s:1},{l:'x2',s:1},{l:'y2',s:1},{l:'x3',s:1},{l:'y3',s:1}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'stroke',l:'Contour',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['20','20','80','20','50','80']
  },
  ellipse: {
    cat: 'shapes', desc: 'Ellipse (2 rayons)', icon: '⬭',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'rx',s:1},{l:'ry',s:1}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'stroke',l:'Contour',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'rotation',l:'Rotation (deg)',t:'number',s:1},{k:'alpha',l:'Opacite',t:'number',s:0.1},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},_BLINK],
    defaults: ['50','50','40','25']
  },
  dot: {
    cat: 'shapes', desc: 'Point avec lueur optionnelle', icon: '●',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:0.5}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','3']
  },
  arc: {
    cat: 'arcs', desc: 'Arc de cercle (contour)', icon: '⌒',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:1},{l:'debut (deg)',s:1},{l:'balayage (deg)',s:1}],
    props: [{k:'stroke',l:'Contour',t:'color'},{k:'fill',l:'Remplissage',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'cap',l:'Terminaison',t:'select',o:['round','butt','square']},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'dash',l:'Tirets',t:'text'},{k:'alpha',l:'Opacite',t:'number',s:0.1},{k:'glow',l:'Lueur',t:'number',s:1},_BLINK],
    defaults: ['50','50','40','-225','270']
  },
  ring: {
    cat: 'arcs', desc: 'Anneau (2 rayons)', icon: '◎',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r ext',s:1},{l:'r int',s:1},{l:'debut (deg)',s:1},{l:'balayage (deg)',s:1}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'stroke',l:'Contour',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','44','36','0','360']
  },
  gradient_ring: {
    cat: 'arcs', desc: 'Anneau avec degrade de couleur', icon: '\u{1F308}',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r ext',s:1},{l:'r int',s:1},{l:'debut (deg)',s:1},{l:'balayage (deg)',s:1},{l:'segments',s:1}],
    props: [{k:'from',l:'Couleur debut',t:'color'},{k:'to',l:'Couleur fin',t:'color'},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','44','38','-225','270','36']
  },
  text: {
    cat: 'text', desc: 'Texte ($value, $unit, $label...)', icon: 'A',
    args: [{l:'contenu',t:'text'},{l:'x',s:1},{l:'y',s:1}],
    props: [{k:'size',l:'Taille',t:'number',s:0.5},{k:'color',l:'Couleur',t:'color'},{k:'bold',l:'Gras',t:'bool'},{k:'italic',l:'Italique',t:'bool'},{k:'align',l:'Alignement',t:'select',o:['center','left','right']},{k:'font',l:'Police',t:'select',o:FONT_OPTIONS},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['$value','50','50']
  },
  ticks: {
    cat: 'ticks', desc: 'Graduations regulieres', icon: '\u2307',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:1},{l:'nombre',s:1},{l:'longueur',s:0.5}],
    props: [{k:'start',l:'Debut (deg)',t:'number',s:1},{k:'sweep',l:'Balayage (deg)',t:'number',s:1},{k:'stroke',l:'Couleur',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','42','11','4']
  },
  sweep_ticks: {
    cat: 'ticks', desc: 'Graduations remplies selon valeur', icon: '\u2307\u25B6',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:1},{l:'nombre',s:1},{l:'longueur',s:0.5}],
    props: [{k:'start',l:'Debut (deg)',t:'number',s:1},{k:'sweep',l:'Balayage (deg)',t:'number',s:1},{k:'on',l:'Couleur active',t:'color'},{k:'off',l:'Couleur inactive',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','48','27','3']
  },
  progress_arc: {
    cat: 'progress', desc: 'Arc qui se remplit avec la valeur', icon: '\u25E0\u25B6',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:1},{l:'debut (deg)',s:1},{l:'balayage max (deg)',s:1}],
    props: [{k:'stroke',l:'Couleur',t:'color'},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'cap',l:'Terminaison',t:'select',o:['round','butt','square']},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},{k:'bg',l:'Fond',t:'color'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','40','-225','270']
  },
  progress_bar: {
    cat: 'progress', desc: 'Barre de progression', icon: '\u25AC\u25B6',
    args: [{l:'x',s:1},{l:'y',s:1},{l:'largeur',s:1},{l:'hauteur',s:1}],
    props: [{k:'dir',l:'Direction',t:'select',o:['right','left','up','down']},{k:'fill',l:'Remplissage',t:'color'},{k:'bg',l:'Fond',t:'color'},{k:'corner',l:'Coins',t:'number',s:0.5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['10','70','80','8']
  },
  progress_ring: {
    cat: 'progress', desc: 'Anneau qui se remplit', icon: '\u25CE\u25B6',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r ext',s:1},{l:'r int',s:1},{l:'debut (deg)',s:1},{l:'balayage max (deg)',s:1}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'bg',l:'Fond',t:'color'},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','44','36','0','360']
  },
  digital_bar: {
    cat: 'progress', desc: 'Barre segmentee LED', icon: '\u25AE\u25AE\u25B6',
    args: [{l:'x',s:1},{l:'y',s:1},{l:'largeur',s:1},{l:'hauteur',s:1},{l:'segments',s:1},{l:'ecart',s:0.5}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'bg',l:'Fond',t:'color'},{k:'corner',l:'Coins',t:'number',s:0.5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['8','70','84','8','30','1']
  },
  needle: {
    cat: 'indicators', desc: 'Aiguille rotative selon valeur', icon: '\u2197',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'longueur',s:1}],
    props: [{k:'from',l:'Debut (deg)',t:'number',s:1},{k:'sweep',l:'Balayage (deg)',t:'number',s:1},{k:'w',l:'Epaisseur',t:'number',s:0.1},{k:'color',l:'Couleur',t:'color'},{k:'tail',l:'Queue',t:'number',s:0.5},{k:'cap',l:'Terminaison',t:'select',o:['round','butt','square']},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'dot',l:'Point central',t:'number',s:0.5},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','34']
  },
  value_dot: {
    cat: 'indicators', desc: 'Point orbitant selon valeur', icon: '\u2299',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r orbite',s:1},{l:'debut (deg)',s:1},{l:'balayage max (deg)',s:1},{l:'r point',s:0.5}],
    props: [{k:'fill',l:'Remplissage',t:'color'},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'glow',l:'Lueur',t:'number',s:1},{k:'glow_color',l:'Couleur lueur',t:'color'},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','44','-225','270','2']
  },
  zone_arc: {
    cat: 'zones', desc: 'Arc colore par zones warn/danger', icon: '\u25E0\u25D0',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r',s:1},{l:'debut (deg)',s:1},{l:'balayage (deg)',s:1},{l:'epaisseur',s:0.5}],
    props: [{k:'normal',l:'Normal',t:'color'},{k:'warning',l:'Warning',t:'color'},{k:'danger',l:'Danger',t:'color'},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','40','-225','270','1.5']
  },
  zone_ring: {
    cat: 'zones', desc: 'Anneau colore par zones', icon: '\u25CE\u25D0',
    args: [{l:'cx',s:1},{l:'cy',s:1},{l:'r ext',s:1},{l:'r int',s:1},{l:'debut (deg)',s:1},{l:'balayage (deg)',s:1}],
    props: [{k:'normal',l:'Normal',t:'color'},{k:'warning',l:'Warning',t:'color'},{k:'danger',l:'Danger',t:'color'},{k:'corner',l:'Forme (0=carre, 100=rond)',t:'number',s:5},{k:'alpha',l:'Opacite',t:'number',s:0.1},_BLINK],
    defaults: ['50','50','44','40','-225','270']
  }
};
