var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import _ from "lodash";
import { JsonAlias, JsonClassType, JsonIgnore, JsonProperty } from "jackson-js";
import { Return } from "./return";
export var OrderItemStatusType;
(function (OrderItemStatusType) {
    OrderItemStatusType["PENDING"] = "PENDING";
    OrderItemStatusType["SHIPPED"] = "SHIPPED";
    OrderItemStatusType["CANCELED"] = "CANCELED";
    OrderItemStatusType["REFUNDED"] = "REFUNDED";
    OrderItemStatusType["RETURNED"] = "RETURNED";
    OrderItemStatusType["REIMBURSED"] = "REIMBURSED";
    OrderItemStatusType["CLAWBACK"] = "CLAWBACK";
})(OrderItemStatusType || (OrderItemStatusType = {}));
export const getPreviousOrderItemStatuses = (status) => {
    switch (status) {
        case OrderItemStatusType.SHIPPED:
            return [OrderItemStatusType.PENDING];
        case OrderItemStatusType.CANCELED:
            return [OrderItemStatusType.PENDING];
        case OrderItemStatusType.REFUNDED:
            return [OrderItemStatusType.PENDING, OrderItemStatusType.SHIPPED];
        case OrderItemStatusType.RETURNED:
            return [OrderItemStatusType.PENDING, OrderItemStatusType.SHIPPED, OrderItemStatusType.REFUNDED];
        case OrderItemStatusType.REIMBURSED:
            return [OrderItemStatusType.PENDING, OrderItemStatusType.SHIPPED, OrderItemStatusType.REFUNDED];
    }
    return [];
};
export class OrderItemStatus {
    constructor(status, date) {
        this.getDate = () => {
            return this.date;
        };
        this.setDate = (date) => {
            this.date = date;
        };
        this.getStatus = () => {
            return this.status;
        };
        this.setStatus = (status) => {
            this.status = status;
        };
        this.status = status;
        this.date = date;
    }
}
__decorate([
    JsonProperty()
], OrderItemStatus.prototype, "status", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Date] })
], OrderItemStatus.prototype, "date", void 0);
export class OrderItem {
    constructor(id, providerId) {
        this.id = id;
        this.providerId = providerId;
        this.sellerSku = "";
        this.currentStatus = "";
        this.orderId = "";
        this.status = [];
        this.updated = false;
    }
    merge(existing) {
        for (const ex of existing.status) {
            if (_.find(this.status, (s) => s.status == ex.status)) {
                continue;
            }
            this.status.push(ex);
            this.updated = true;
        }
        this.setCurrentStatus(existing.currentStatus);
    }
    hasBeenUpdated() {
        return this.updated;
    }
    setUpdated() {
        this.updated = true;
    }
    setCurrentStatus(status) {
        if (!getPreviousOrderItemStatuses(this.currentStatus).includes(status)) {
            this.currentStatus = status;
            this.updated = true;
        }
    }
    getCurrentStatus() {
        return this.currentStatus;
    }
    addStatus(status) {
        this.status.push(status);
        this.updated = true;
    }
    isReturnable() {
        return this.currentStatus == OrderItemStatusType.SHIPPED;
    }
    hasStatus(status) {
        return this.getStatus(status) != undefined;
    }
    getStatus(status) {
        return _.find(this.status, (s) => s.status == status);
    }
}
__decorate([
    JsonProperty(),
    JsonAlias({ values: ['id', 'orderItemId'] })
], OrderItem.prototype, "id", void 0);
__decorate([
    JsonProperty()
], OrderItem.prototype, "orderId", void 0);
__decorate([
    JsonProperty()
], OrderItem.prototype, "providerId", void 0);
__decorate([
    JsonProperty()
], OrderItem.prototype, "sellerSku", void 0);
__decorate([
    JsonProperty()
], OrderItem.prototype, "currentStatus", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Array, [OrderItemStatus]] || [OrderItemStatus] }),
    JsonAlias({ values: ['status', 'statusHistory'] })
], OrderItem.prototype, "status", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Object] })
], OrderItem.prototype, "attributes", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Return] })
], OrderItem.prototype, "return", void 0);
__decorate([
    JsonIgnore()
], OrderItem.prototype, "updated", void 0);
export class Order {
    constructor(id, provider, providerId) {
        this.id = id;
        this.provider = provider;
        this.providerId = providerId;
        this.marketplaceId = "";
        this.purchaseDate = new Date();
        this.lastUpdateDate = new Date();
        this.orderItems = [];
        this.updated = false;
    }
    merge(existing) {
        this.updated = true;
        for (const item of existing.orderItems) {
            const thisItem = _.find(this.orderItems, (i) => i.id == item.id);
            if (thisItem) {
                thisItem.merge(item);
            }
            else {
                this.pushItems(item);
            }
        }
    }
    hasBeenUpdated() {
        return this.updated;
    }
    setUpdated() {
        this.updated = true;
    }
    pushItems(...item) {
        this.orderItems.push(...item);
    }
}
__decorate([
    JsonProperty()
], Order.prototype, "id", void 0);
__decorate([
    JsonProperty()
], Order.prototype, "provider", void 0);
__decorate([
    JsonProperty()
], Order.prototype, "providerId", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Date] })
], Order.prototype, "purchaseDate", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Date] })
], Order.prototype, "lastUpdateDate", void 0);
__decorate([
    JsonProperty(),
    JsonAlias({ values: ['marketplaceId', 'marketplace'] })
], Order.prototype, "marketplaceId", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Object] })
], Order.prototype, "attributes", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Object] })
], Order.prototype, "shippingAddress", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Object] })
], Order.prototype, "buyer", void 0);
__decorate([
    JsonProperty(),
    JsonClassType({ type: () => [Array, [OrderItem]] }),
    JsonAlias({ values: ['items', 'orderItems'] })
], Order.prototype, "orderItems", void 0);
__decorate([
    JsonIgnore()
], Order.prototype, "updated", void 0);
//# sourceMappingURL=order.js.map