// @flow

import uuid from 'uuid/v4'
import {randomChar} from '@gob/lib'

import type {Course} from '@gob/types'

export type ScheduleType = {
	+id: string,
	+active: boolean,
	+index: number,
	+title: string,
	+clbids: Array<string>,
	+year: number,
	+semester: number,
	+metadata: Object,
}

export type HydratedScheduleType = {
	...$Exact<ScheduleType>,
	courses: Array<Course>,
	conflicts: Array<mixed>,
	hasConflict: boolean,
}

type InputSchedule = {
	id?: string,
	active?: boolean,
	index?: number,
	title?: string,
	clbids?: Array<number | string>,
	year?: string | number,
	semester?: string | number,
	metadata?: Object,
}

export function Schedule(data: InputSchedule = {}): ScheduleType {
	const baseSchedule = {
		id: uuid(),
		active: false,

		index: 1,
		title: `Schedule ${randomChar().toUpperCase()}`,

		clbids: [],
		year: 0,
		semester: 0,

		metadata: {},
	}

	let schedule = {...baseSchedule, ...data}

	let id = schedule.id
	if (typeof id !== 'string') {
		throw new TypeError('Schedule id must be a string.')
	}

	let clbids = schedule.clbids
	if (clbids.some(id => typeof id === 'number')) {
		clbids = clbids.map(
			id => (typeof id !== 'string' ? String(id).padStart(10, '0') : id),
		)
	}

	let year = schedule.year
	if (typeof year === 'string') {
		year = parseInt(year, 10)
	}

	let semester = schedule.semester
	if (typeof semester === 'string') {
		semester = parseInt(semester, 10)
	}

	return {
		...(schedule: any),
		id: id,
		year: year,
		semester: semester,
		clbids: ((clbids: any): Array<string>),
	}
}

@@ -67,16 +67,68 @@
 b/lib'%0A%0A
+import type %7BWarning%7D from './find-course-warnings'%0A
 import t
@@ -449,13 +449,23 @@
 ray%3C
-mixed
+Array%3C?Warning%3E
 %3E,%0A%09

clarify type of `conflicts` in HydratedSchedule