(function (mod) {
  if (typeof exports == "object" && typeof module == "object") // CommonJS
    mod(require("../../lib/codemirror"));
  else if (typeof define == "function" && define.amd) // AMD
    define(["../../lib/codemirror"], mod);
  else // Plain browser env
    mod(CodeMirror);
})(function (CodeMirror) {
  "use strict";

  CodeMirror.defineMode("org", function () {
    return {
      startState: function () {
        return {
          inComment: false,
          inBlock: false,
          blockType: null
        };
      },

      token: function (stream, state) {
        // Gestion des commentaires en ligne
        if (stream.match(/^# /)) {
          stream.skipToEnd();
          return "comment";
        }

        // Gestion des lignes de paramètres
        if (stream.match(/^:PROPERTIES:|^:END:|^:ID:|^#\+[A-Z_]+:/)) {
          stream.skipToEnd();
          return "comment";
        }

        // Gestion des paramètres d'export
        if (stream.match(/^:title|^:author|^:email|^:language|^:encoding|^:subject|^:description|^:keywords|^:cover/)) {
          stream.skipToEnd();
          return "comment";
        }

        // Gestion des blocs de commentaires
        if (stream.match(/^#\+begin_comment/)) {
          state.inComment = true;
          return "comment";
        }

        if (state.inComment && stream.match(/^#\+end_comment/)) {
          state.inComment = false;
          return "comment";
        }

        if (state.inComment) {
          stream.skipToEnd();
          return "comment";
        }

        // Gestion des blocs de code
        if (stream.match(/^#\+BEGIN_SRC/)) {
          state.inBlock = true;
          state.blockType = "src";
          return "keyword";
        }

        if (stream.match(/^#\+BEGIN_QUOTE/)) {
          state.inBlock = true;
          state.blockType = "quote";
          return "keyword";
        }

        if (state.inBlock && stream.match(/^#\+END_(SRC|QUOTE)/)) {
          state.inBlock = false;
          state.blockType = null;
          return "keyword";
        }

        if (state.inBlock) {
          stream.skipToEnd();
          return state.blockType === "src" ? "string" : "quote";
        }

        // Gestion des titres
        if (stream.match(/^\*+\s/)) {
          const level = stream.current().length - 1;
          stream.skipToEnd();
          return `header-${level}`;
        }

        // Gestion des listes
        if (stream.match(/^-\s/)) {
          stream.skipToEnd();
          return "list";
        }

        // Gestion des liens
        if (stream.match(/\[\[(.*?)\]\]/)) {
          return "link";
        }

        // Gestion des propriétés
        if (stream.match(/^:PROPERTIES:/)) {
          stream.skipToEnd();
          return "property";
        }

        if (stream.match(/^:END:/)) {
          stream.skipToEnd();
          return "property";
        }

        // Gestion des tags
        if (stream.match(/:[a-zA-Z0-9_@#%:]+:/)) {
          return "tag";
        }

        // Gestion des dates
        if (stream.match(/<\d{4}-\d{2}-\d{2}(?: \w+)?>/)) {
          return "date";
        }

        // Gestion des mots-clés
        if (stream.match(/^#\+[A-Z_]+:/)) {
          stream.skipToEnd();
          return "keyword";
        }

        // Gestion des nombres
        if (stream.match(/\b\d+\b/)) {
          return "number";
        }

        // Texte normal
        stream.next();
        return null;
      }
    };
  });

  CodeMirror.defineMIME("text/x-org", "org");
}); 