"use strict";
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Order = exports.OrderItem = exports.OrderItemStatus = exports.getPreviousOrderItemStatuses = exports.OrderItemStatusType = void 0;
const lodash_1 = __importDefault(require("lodash"));
const jackson_js_1 = require("jackson-js");
const return_1 = require("./return");
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 = exports.OrderItemStatusType || (exports.OrderItemStatusType = {}));
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 [];
};
exports.getPreviousOrderItemStatuses = getPreviousOrderItemStatuses;
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([
    (0, jackson_js_1.JsonProperty)()
], OrderItemStatus.prototype, "status", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Date] })
], OrderItemStatus.prototype, "date", void 0);
exports.OrderItemStatus = OrderItemStatus;
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 (lodash_1.default.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 (!(0, exports.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 lodash_1.default.find(this.status, (s) => s.status == status);
    }
}
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonAlias)({ values: ['id', 'orderItemId'] })
], OrderItem.prototype, "id", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], OrderItem.prototype, "orderId", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], OrderItem.prototype, "providerId", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], OrderItem.prototype, "sellerSku", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], OrderItem.prototype, "currentStatus", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Array, [OrderItemStatus]] || [OrderItemStatus] }),
    (0, jackson_js_1.JsonAlias)({ values: ['status', 'statusHistory'] })
], OrderItem.prototype, "status", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Object] })
], OrderItem.prototype, "attributes", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [return_1.Return] })
], OrderItem.prototype, "return", void 0);
__decorate([
    (0, jackson_js_1.JsonIgnore)()
], OrderItem.prototype, "updated", void 0);
exports.OrderItem = OrderItem;
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 = lodash_1.default.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([
    (0, jackson_js_1.JsonProperty)()
], Order.prototype, "id", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], Order.prototype, "provider", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)()
], Order.prototype, "providerId", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Date] })
], Order.prototype, "purchaseDate", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Date] })
], Order.prototype, "lastUpdateDate", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonAlias)({ values: ['marketplaceId', 'marketplace'] })
], Order.prototype, "marketplaceId", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Object] })
], Order.prototype, "attributes", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Object] })
], Order.prototype, "shippingAddress", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Object] })
], Order.prototype, "buyer", void 0);
__decorate([
    (0, jackson_js_1.JsonProperty)(),
    (0, jackson_js_1.JsonClassType)({ type: () => [Array, [OrderItem]] }),
    (0, jackson_js_1.JsonAlias)({ values: ['items', 'orderItems'] })
], Order.prototype, "orderItems", void 0);
__decorate([
    (0, jackson_js_1.JsonIgnore)()
], Order.prototype, "updated", void 0);
exports.Order = Order;
//# sourceMappingURL=order.js.map