import type { ScheduleEditorForm } from '../../types';

/**
 * 文件职责：时段供应页面常量。
 */

/** 星期选项。 */
export const WEEK_DAY_OPTIONS = [
  { label: '周一', value: 1 },
  { label: '周二', value: 2 },
  { label: '周三', value: 3 },
  { label: '周四', value: 4 },
  { label: '周五', value: 5 },
  { label: '周六', value: 6 },
  { label: '周日', value: 7 },
] as const;

/** 全周。 */
export const WEEK_DAYS_ALL = [1, 2, 3, 4, 5, 6, 7] as const;

/** 工作日。 */
export const WEEK_DAYS_WEEKDAY = [1, 2, 3, 4, 5] as const;

/** 周末。 */
export const WEEK_DAYS_WEEKEND = [6, 7] as const;

/** 时间轴刻度。 */
export const TIMELINE_AXIS_TICKS = [0, 3, 6, 9, 12, 15, 18, 21, 24] as const;

/** 规则颜色池。 */
export const SCHEDULE_COLOR_PALETTE = [
  '#1890ff',
  '#52c41a',
  '#722ed1',
  '#fa8c16',
  '#13c2c2',
  '#eb2f96',
  '#2f54eb',
] as const;

const DEFAULT_SCHEDULE_COLOR = '#1890ff';

/** 创建默认编辑表单。 */
export function createDefaultScheduleForm(): ScheduleEditorForm {
  return {
    id: '',
    name: '',
    startTime: '06:00',
    endTime: '10:00',
    weekDays: [...WEEK_DAYS_ALL],
    productIds: [],
    status: 'enabled',
  };
}

/** 归一化星期列表。 */
export function normalizeWeekDays(weekDays: number[]) {
  const next = weekDays
    .map(Number)
    .filter((item) => Number.isInteger(item) && item >= 1 && item <= 7)
    .toSorted((a, b) => a - b);
  return [...new Set(next)];
}

/** 按规则 ID 生成稳定颜色。 */
export function resolveScheduleColor(scheduleId: string) {
  if (!scheduleId) {
    return DEFAULT_SCHEDULE_COLOR;
  }

  let hash = 0;
  for (const char of scheduleId) {
    hash = (hash * 31 + (char.codePointAt(0) ?? 0)) >>> 0;
  }

  return (
    SCHEDULE_COLOR_PALETTE[hash % SCHEDULE_COLOR_PALETTE.length] ??
    DEFAULT_SCHEDULE_COLOR
  );
}
