import { BaseDataDefinition } from "./BaseDataDefinition";
import { XDataType } from "../XDataType";
import { ValidationIssue } from "../../IDesignValidationProvider";
import { parseISO, parseJSON } from "date-fns";
export class DateDataDefinition extends BaseDataDefinition {
    constructor() {
        super(...arguments);
        this.type = XDataType.DATE;
        this.storeTime = true;
    }
    getStorageData() {
        let object = super.getStorageData();
        Reflect.set(object, "lowerBounds", this.lowerBounds);
        Reflect.set(object, "upperBounds", this.upperBounds);
        Reflect.set(object, "storeTime", this.storeTime);
        return object;
    }
    setStorageData(data) {
        super.setStorageData(data);
        let lowerBoundsVal = Reflect.get(data, "lowerBounds");
        if (lowerBoundsVal)
            this.lowerBounds = new Date(lowerBoundsVal);
        let upperBoundsVal = Reflect.get(data, "upperBounds");
        if (upperBoundsVal)
            this.upperBounds = new Date(upperBoundsVal);
        this.storeTime = Reflect.get(data, "storeTime");
    }
    verifyValueType(value) {
        if (value === undefined)
            return new ValidationIssue("Undefined Value");
        if (this.list) {
            // check is array
            if (!Array.isArray(value))
                return new ValidationIssue(value + " is not a date array");
            // check each value is of right type
            for (let n of value) {
                if (!this.isDate(n))
                    return new ValidationIssue(value + " has array members that are not dates");
            }
        }
        else {
            if (Array.isArray(value))
                return new ValidationIssue(value + " is a date array");
            if (!this.isDate(value))
                return new ValidationIssue(value + " is not a date");
        }
        return undefined;
    }
    isDate(v) {
        if (typeof v === "string") {
            let d = new Date(v);
            return !isNaN(d.getTime());
        }
        else if (v instanceof Date) {
            return !isNaN(v.getTime());
        }
        return false;
    }
    convert(value) {
        if (value === undefined)
            return undefined;
        if (this.list) {
            if (Array.isArray(value)) {
                let output = [];
                for (let n of value) {
                    if (n instanceof Date) {
                        output.push(n);
                    }
                    else {
                        let d = this.parseDate(n);
                        if (d)
                            output.push(d);
                        else
                            output.push(n); // put original object in
                    }
                }
                return output;
            }
            return value;
        }
        else {
            if (value instanceof Date) {
                return value;
            }
            else {
                let d = this.parseDate(value);
                if (d)
                    return d;
                else
                    return value; // return original value
            }
        }
    }
    parseDate(value) {
        if (value === undefined)
            return undefined;
        if (typeof value === "number") {
            return undefined;
        }
        if (value instanceof Date) {
            return value;
        }
        if (value === null)
            return undefined;
        // todo: expand date parsing logic
        let d = parseISO(value.toString());
        if (d.getFullYear())
            return d;
        d = parseJSON(value.toString());
        if (d.getFullYear())
            return d;
        return undefined;
    }
}
//# sourceMappingURL=DateDataDefinition.js.map