//HTMLElements
//Cart Modal
class OrderItemTableRow {
    constructor(element) {
        this.element = element;
    }
    get Index() { return Number(this.element.getAttribute("data-selecteditemindex")); }
    get Count() { return Number(this.element.cells[2].textContent); }
    get Price() { return Number(this.element.cells[3].textContent); }
}
//Item Panel
class OrderItemInput {
    constructor(input) {
        if (input == null || !input.hasAttribute("orderitemid"))
            throw new Error("Invalid input element");
        this.element = input;
    }
    get InputValue() { return Number(this.element.value); }
    set InputValue(value) { this.element.value = value.toString(); }
    get ValueBefore() { return Number(this.element.getAttribute("value-before")); }
    set ValueBefore(value) { this.element.setAttribute("value-before", value.toString()); }
    get Value() { return this.InputValue; }
    get MinSelectedCount() { return 0; }
    get MaxSelectedCount() { if (this.ItemMaxCount > 0)
        return this.ItemMaxCount; return -1; }
    get ItemID() { var _a; return (_a = this.element.getAttribute("orderitemid")) !== null && _a !== void 0 ? _a : ""; }
    get Price() { return Number(this.element.getAttribute("item-price")); }
    get Deposit() { return this.element.hasAttribute("item-deposit") ? Number(this.element.getAttribute("item-deposit")) : undefined; }
    get ItemMaxCount() { return Number(this.element.getAttribute("item-maxcount")); }
    get ItemHasOptions() { return this.element.getAttribute("item-hasoptions") == "true"; }
    Initialize(count) {
        $(this.element).bootstrapNumber();
        //console.log(count);
        this.InputValue = count !== null && count !== void 0 ? count : 0;
        //console.log(this.InputValue);
        //this.ValueBefore = this.InputValue;
    }
}
class TicketItemInput extends OrderItemInput {
    constructor(input) {
        super(input);
    }
    get Value() { return this.InputValue * this.TicketCount; }
    get MinSelectedCount() { return Math.ceil(this.TicketMinCount / this.TicketCount); }
    get MaxSelectedCount() {
        let ticketCount = 0;
        if (this.TicketValidate) {
            if (this.TicketMaxCount < 1)
                return 0;
            ticketCount = this.GetTicketCount();
        }
        return this.GetMaxTicketItemCount(ticketCount);
    }
    get TicketCategoryID() { return this.element.getAttribute("ticketcategoryid"); }
    get TicketCount() { return Math.max(Number(this.element.getAttribute("ticket-count")), 1); }
    get TicketMinCount() { return Number(this.element.getAttribute("ticket-mincount")); }
    get TicketMaxCount() { return Number(this.element.getAttribute("ticket-maxcount")); }
    set TicketMaxCount(value) { this.element.setAttribute("ticket-maxcount", value.toString()); }
    get TicketValidate() { return this.element.getAttribute("ticket-validate") == "true"; }
    get TicketEntryMode() { return this.element.getAttribute("ticket-entrymode"); }
    get TicketEntryStart() { return this.element.getAttribute("ticket-entrystart"); }
    get TicketEntryEnd() { return this.element.getAttribute("ticket-entryend"); }
    set TicketLeftCount(value) { var _a, _b; ((_b = (_a = this.element.parentElement) === null || _a === void 0 ? void 0 : _a.parentElement) === null || _b === void 0 ? void 0 : _b.querySelector("span.orderItemCounter")).innerHTML = value.toString(); }
    //override Initialize() {
    //    super.Initialize();
    //    //this.UpdateLeftTicketCount();
    //}
    GetTicketCategoryItems(includeSelf = false) {
        let items = GetAllItemInputs().map(x => x).filter(x => x.TicketCategoryID == this.TicketCategoryID);
        if (!includeSelf)
            items = items.filter(x => x.ItemID != this.ItemID);
        switch (this.TicketEntryMode) {
            case "2":
                //items = items.filter(x => x.TicketEntryMode == "2" && x.TicketEntryStart == this.TicketEntryStart && x.TicketEntryEnd == this.TicketEntryEnd);
                items = items.filter(x => x.TicketEntryMode == "2" && ((x.TicketEntryStart >= this.TicketEntryStart && x.TicketEntryStart < this.TicketEntryEnd) || (x.TicketEntryEnd > this.TicketEntryStart && x.TicketEntryEnd <= this.TicketEntryEnd)));
                break;
            default:
                items = items.filter(x => x.TicketEntryMode != "2");
                break;
        }
        return items;
    }
    GetTicketCount(includeSelf = false) {
        return this.GetTicketCategoryItems(includeSelf).reduce((a, b) => a + b.Value, 0);
    }
    GetMaxTicketItemCount(ticketCount = 0, subtractTicketCount = true) {
        if (this.TicketValidate) {
            if (this.TicketMaxCount < 1)
                return 0;
            let leftTickets;
            if (subtractTicketCount)
                leftTickets = Math.max(this.TicketMaxCount - ticketCount, 0);
            else
                leftTickets = Math.max(this.TicketMaxCount, 0);
            if (leftTickets < this.TicketCount)
                return 0;
            if (this.ItemMaxCount > 0)
                return Math.min(this.ItemMaxCount, Math.floor(leftTickets / this.TicketCount));
            return Math.floor(leftTickets / this.TicketCount);
        }
        if (this.ItemMaxCount > 0)
            return this.ItemMaxCount;
        return -1;
    }
    UpdateLeftTicketCount() {
        let ticketCount = this.GetTicketCount(true);
        this.GetTicketCategoryItems(true).forEach(function (ticket) {
            if (ticket.TicketValidate) {
                let newLeftCount = ticket.GetMaxTicketItemCount(ticketCount - ticket.Value, false) - ticket.InputValue - Math.ceil((ticketCount - ticket.Value) / ticket.TicketCount);
                if (newLeftCount < 0)
                    newLeftCount = 0;
                ticket.TicketLeftCount = newLeftCount;
                //ticket.TicketLeftCount = ticket.GetMaxTicketItemCount(ticketCount - ticket.Value) - ticket.Value - (ticketCount - ticket.Value);
            }
        });
    }
}
//Item Option Modal
class ItemOptionElement {
    constructor(input) {
        if (input == null || !input.hasAttribute("data-option"))
            throw new Error("Invalid element");
        this.element = input;
    }
    get OptionID() { return this.element.getAttribute("data-option"); }
    get Variants() { return Array.from(this.element.querySelectorAll("span[data-variant]")).map(x => new OptionVariantElement(x)); }
    get Max() { return Number(this.element.getAttribute("data-max")); }
    get Min() { return Number(this.element.getAttribute("data-min")); }
    get HasTrigger() { return this.element.hasAttribute("data-triggered"); }
    get Triggered() { return this.element.getAttribute("data-triggered") !== "false"; }
    set Triggered(triggered) { if (this.HasTrigger)
        this.element.setAttribute("data-triggered", triggered ? "true" : "false"); }
    get SelectedVariants() { return this.Variants.filter(x => x.Selected); }
    get IsSelected() { return this.Triggered && this.SelectedVariants.length > 0; }
}
class OptionVariantElement {
    constructor(input) {
        if (input == null || !input.hasAttribute("data-variant"))
            throw new Error("Invalid element");
        this.element = input;
    }
    get VariantID() { return this.element.getAttribute("data-variant"); }
    get Price() { return Number(this.element.getAttribute("data-price")); }
    get Text() { var _a; return (_a = this.element.querySelector("input[type=text]")) === null || _a === void 0 ? void 0 : _a.value; }
    get CheckBox() { return this.element.querySelector("input[type=checkbox]"); }
    get Selected() { return this.CheckBox.checked; }
    set Selected(selected) { this.CheckBox.checked = selected; }
    get TriggeredOption() { return this.element.getAttribute("data-triggersoption"); }
    get OptionElement() { return new ItemOptionElement(this.element.parentElement); }
}
//Selected
class SelectedOrderItem {
    constructor(item) {
        this.Index = item.Index;
        this.ItemID = item.ItemID;
        this.Count = item.Count;
        this.Price = item.Price;
        this.SelectedDate = item.SelectedDate;
        this.Options = item.Options;
        this.Deposit = item.Deposit;
    }
    GetTotalPrice() {
        var _a;
        let price = this.Price + ((_a = this.Deposit) !== null && _a !== void 0 ? _a : 0);
        if (this.Options != null)
            price += this.Options.reduce((a, o) => a + o.Variants.reduce((b, v) => b + v.Price, 0), 0);
        return this.Count * price;
    }
    Equals(compare, ignoreOptions = false) {
        if (this.ItemID !== compare.ItemID)
            return false;
        if (this.Price !== compare.Price)
            return false;
        if (JSON.stringify(this.SelectedDate) !== JSON.stringify(compare.SelectedDate))
            return false;
        if (!ignoreOptions && JSON.stringify(this.Options) !== JSON.stringify(compare.Options))
            return false;
        return true;
        //let clone = Clone(this);
        //let cloneCompare = Clone(compare);
        //delete clone.Index;
        //delete clone.Count;
        //delete cloneCompare.Index;
        //delete cloneCompare.Count;
        //if (ignoreOptions) {
        //    delete clone.Options;
        //    delete cloneCompare.Options;
        //}
        ////console.log(`ignoreOptions: ${ignoreOptions}\nclone: ${clone}\n${JSON.stringify(clone)}\ncloneCompare: ${cloneCompare}\n${JSON.stringify(cloneCompare)}\nequal: ${JSON.stringify(clone) == JSON.stringify(cloneCompare) }`);
        //return JSON.stringify(clone) == JSON.stringify(cloneCompare);
    }
}
class SelectedItemOption {
    constructor(option) {
        this.OptionID = option.OptionID;
        this.Variants = option.Variants.map(x => new SelectedOptionVariant(x));
    }
}
class SelectedOptionVariant {
    constructor(variant) {
        this.VariantID = variant.VariantID;
        this.Price = variant.Price;
        this.Text = variant.Text;
    }
}
//Abstract
class OrderItem {
}
class ItemOption {
}
class OptionVariant {
}
//# sourceMappingURL=OrderItem.js.map