(function () {
  function cleanClassFields(input) {
    const source = input || {};
    const trimmed = {
      name: typeof source.name === 'string' ? source.name.trim() : '',
      course_id: typeof source.course_id === 'string' ? source.course_id.trim() : '',
      teacher_id: typeof source.teacher_id === 'string' ? source.teacher_id.trim() : '',
      classroom_id: typeof source.classroom_id === 'string' ? source.classroom_id.trim() : '',
      start_date: typeof source.start_date === 'string' ? source.start_date.trim() : '',
      end_date: typeof source.end_date === 'string' ? source.end_date.trim() : ''
    };
    const numericMax = Number(source.max_students);
    trimmed.max_students = Number.isFinite(numericMax) ? Math.trunc(numericMax) : 0;
    const numericStatus = Number(source.status);
    trimmed.status = numericStatus === 0 ? 0 : 1;
    return trimmed;
  }

  function isIsoDate(value) {
    return typeof value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(value);
  }

  function validateClass(input, ctx) {
    const errors = {};
    const value = input || {};
    const ctxMappings = (ctx && ctx.mappings) || {};
    const mappings = {
      courses: ctxMappings.courses instanceof Map ? ctxMappings.courses : new Map(),
      teachers: ctxMappings.teachers instanceof Map ? ctxMappings.teachers : new Map(),
      classrooms: ctxMappings.classrooms instanceof Map ? ctxMappings.classrooms : new Map()
    };
    const name = typeof value.name === 'string' ? value.name.trim() : '';
    if (!name) {
      errors.name = '名称为必填项';
    } else if (name.length > 50) {
      errors.name = '名称长度不能超过 50';
    }
    if (value.course_id && !mappings.courses.has(value.course_id)) {
      errors.course_id = '请重新选择课程';
    }
    if (!value.teacher_id || !mappings.teachers.has(value.teacher_id)) {
      errors.teacher_id = '请重新选择教师';
    }
    if (!value.classroom_id || !mappings.classrooms.has(value.classroom_id)) {
      errors.classroom_id = '请重新选择教室';
    }
    if (!isIsoDate(value.start_date)) {
      errors.start_date = '开始日期格式不正确';
    }
    if (!isIsoDate(value.end_date)) {
      errors.end_date = '结束日期格式不正确';
    } else if (isIsoDate(value.start_date) && value.end_date < value.start_date) {
      errors.end_date = '结束日期不能早于开始日期';
    }
    const max = Number(value.max_students);
    if (!Number.isInteger(max) || max < 1 || max > 500) {
      errors.max_students = '最大人数需为 1–500 的整数';
    }
    if (value.status !== 0 && value.status !== 1) {
      errors.status = '状态值不合法';
    }
    return errors;
  }

  function summarizeReferences(counts) {
    const schedulesTotal = (counts && counts.schedules && counts.schedules.total) || 0;
    const totalCount = schedulesTotal;
    const parts = [];
    if (schedulesTotal > 0) parts.push(`排期 ${schedulesTotal} 个`);
    return {
      hasReferences: totalCount > 0,
      totalCount,
      schedulesTotal,
      message: parts.join('、')
    };
  }

  const exports_ = { cleanClassFields, validateClass, summarizeReferences };

  if (typeof module !== 'undefined' && module.exports) {
    module.exports = exports_;
  }

  if (typeof window !== 'undefined') {
    window.classes = exports_;
  }
})();