// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE

// Depends on csslint.js from https://github.com/stubbornella/csslint

// declare global: CSSLint
/**
 * 模块封装：兼容 CommonJS/AMD/浏览器全局环境，确保代码在不同环境下可正常加载
 * @param {Function} mod - 模块函数，接收 CodeMirror 作为参数
 */
(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";
/**
   * 注册 CSS 语法检查助手（lint 类型为 "css"）
   * 功能：调用 CSSLint 工具检查 CSS 代码，将结果转换为 CodeMirror 可识别的格式
   * @param {string} text - 待检查的 CSS 代码文本（从编辑器中获取）
   * @return {Array} 语法/规范问题列表，每个元素包含问题位置、信息、严重程度
   */
CodeMirror.registerHelper("lint", "css", function(text) {
  var found = [];
  if (!window.CSSLint) return found;
  var results = CSSLint.verify(text), messages = results.messages, message = null;
  for ( var i = 0; i < messages.length; i++) {
    message = messages[i];
    var startLine = message.line -1, endLine = message.line -1, startCol = message.col -1, endCol = message.col;
    found.push({
      from: CodeMirror.Pos(startLine, startCol),
      to: CodeMirror.Pos(endLine, endCol),
      message: message.message,
      severity : message.type
    });
  }
  return found;
});

});
