import DatabaseProperty from "../Database/DatabaseProperty";
import OneUptimeDate from "../Date";
import DayOfWeek from "../Day/DayOfWeek";
import BadDataException from "../Exception/BadDataException";
import { ObjectType } from "../JSON";
import JSONFunctions from "../JSONFunctions";
export var RestrictionType;
(function (RestrictionType) {
    RestrictionType["Daily"] = "Daily";
    RestrictionType["Weekly"] = "Weekly";
    RestrictionType["None"] = "None";
})(RestrictionType || (RestrictionType = {}));
export default class RestrictionTimes extends DatabaseProperty {
    get restictionType() {
        return this.data.restictionType;
    }
    set restictionType(v) {
        this.data.restictionType = v;
    }
    // dayRestrictionTimes
    get dayRestrictionTimes() {
        return this.data.dayRestrictionTimes;
    }
    set dayRestrictionTimes(v) {
        this.data.dayRestrictionTimes = v;
    }
    // weeklyRestrictionTimes
    get weeklyRestrictionTimes() {
        return this.data.weeklyRestrictionTimes;
    }
    set weeklyRestrictionTimes(v) {
        this.data.weeklyRestrictionTimes = v;
    }
    constructor() {
        super();
        this.data = RestrictionTimes.getDefaultRestrictonTimeData();
        this.data = RestrictionTimes.getDefaultRestrictonTimeData();
    }
    static getDefaultRestrictonTimeData() {
        return {
            restictionType: RestrictionType.None,
            dayRestrictionTimes: null,
            weeklyRestrictionTimes: [],
        };
    }
    static getDefault() {
        return new RestrictionTimes();
    }
    toJSON() {
        return JSONFunctions.serialize({
            _type: ObjectType.RestrictionTimes,
            value: {
                restictionType: this.restictionType,
                dayRestrictionTimes: this.dayRestrictionTimes,
                weeklyRestrictionTimes: this.weeklyRestrictionTimes,
            },
        });
    }
    static fromJSON(json) {
        if (json instanceof RestrictionTimes) {
            return json;
        }
        if (!json || json["_type"] !== ObjectType.RestrictionTimes) {
            throw new BadDataException("Invalid Restriction Times");
        }
        if (!json["value"]) {
            throw new BadDataException("Invalid Restriction Times");
        }
        const data = json["value"];
        const restrictionTimes = new RestrictionTimes();
        restrictionTimes.restictionType = data["restictionType"];
        restrictionTimes.dayRestrictionTimes = data["dayRestrictionTimes"];
        const weeklyRestrictionTimes = data["weeklyRestrictionTimes"] || {};
        restrictionTimes.weeklyRestrictionTimes = weeklyRestrictionTimes;
        return restrictionTimes;
    }
    removeAllRestrictions() {
        this.restictionType = RestrictionType.None;
        this.dayRestrictionTimes = null;
        this.weeklyRestrictionTimes = [];
    }
    addDefaultDailyRestriction() {
        this.restictionType = RestrictionType.Daily;
        this.dayRestrictionTimes = {
            startTime: OneUptimeDate.getDateWithCustomTime({
                hours: 0,
                minutes: 0,
                seconds: 0,
            }),
            endTime: OneUptimeDate.getDateWithCustomTime({
                hours: 1,
                minutes: 0,
                seconds: 0,
            }),
        };
        this.weeklyRestrictionTimes = [];
    }
    addDefaultWeeklyRestriction() {
        this.restictionType = RestrictionType.Weekly;
        this.dayRestrictionTimes = null;
        this.weeklyRestrictionTimes = [
            RestrictionTimes.getDefaultWeeklyRestrictionTIme(),
        ];
    }
    static getDefaultWeeklyRestrictionTIme() {
        return {
            startDay: DayOfWeek.Sunday,
            endDay: DayOfWeek.Monday,
            startTime: OneUptimeDate.getDateWithCustomTime({
                hours: 0,
                minutes: 0,
                seconds: 0,
            }),
            endTime: OneUptimeDate.getDateWithCustomTime({
                hours: 1,
                minutes: 0,
                seconds: 0,
            }),
        };
    }
    static toDatabase(value) {
        if (value && value instanceof RestrictionTimes) {
            return value.toJSON();
        }
        else if (value) {
            return JSONFunctions.serialize(value);
        }
        return null;
    }
    static fromDatabase(value) {
        if (value) {
            return RestrictionTimes.fromJSON(value);
        }
        return null;
    }
}
//# sourceMappingURL=RestrictionTimes.js.map