(function () {
    "use strict";

    let terms = window.productTerms;

    const DisplayType = {
        NORMAL: 0,
        BUNDLE: 1,
        BUDDY: 2,
        PRESELL: 3,
        SET: 4
    };

    const Availability = {
        AVAILABLE: 0,
        OUT_OF_STOCK: 1,
        BACKORDER_ALLOW_ORDERING: 2,
        PWS_BACKORDER_OVERRIDE: 3,
        COMING_SOON: 4,
        BACKORDER_DO_NOT_ALLOW_ORDERING: 5,
        CLUB_EXCLUSIVE: 6,
        INACTIVE: 7,
        SOLD_OUT: 8,
        CLUB_EXCLUSIVE_OUT_OF_STOCK: 9,
        CLUB_EXCLUSIVE_SOLD_OUT: 10
    };

    const FooterType = window.FooterType = {
        LINK: "link",
        ADD_TO_CART: "addToCart",
        ADD_BUDDY_TO_CART: "addBuddyToCart",
        REMOVE_FROM_LIST: "removeFromWishList"
    };

    let viewDetailsButtonText = ko.pureComputed(() => responsiveState() === BREAKPOINT_MOBILE
        ? terms.viewDetailsMobileBtnText
        : terms.viewDetailsBtnText);

    let addToCartButtonText = ko.pureComputed(() => responsiveState() === BREAKPOINT_MOBILE
        ? ""
        : terms.addToCart);

    window.ProductViewModel = function (product, forceSmall = false, shouldShowBagSidebar = true, callbackFunction = null, childProducts = []) {
        const productAvailability = product.productProperties.availability;
        const productDisplayType = product.productProperties.displayType;
        switch (productAvailability) {
            case Availability.OUT_OF_STOCK:
            case Availability.CLUB_EXCLUSIVE_OUT_OF_STOCK:
                product.badgeText = terms.outOfStock;
                product.badgeClass = "badge-danger";
                break;
            case Availability.SOLD_OUT:
            case Availability.CLUB_EXCLUSIVE_SOLD_OUT:
                product.badgeText = terms.soldOut;
                product.badgeClass = "badge-danger";
                break;
            case Availability.BACKORDER_ALLOW_ORDERING:
            case Availability.PWS_BACKORDER_OVERRIDE:
            case Availability.BACKORDER_DO_NOT_ALLOW_ORDERING:
                product.badgeText = terms.onBackOrder;
                product.badgeClass = "badge-danger";
                break;
            case Availability.COMING_SOON:
                product.badgeText = terms.comingSoon;
                product.badgeClass = "badge-info";
                break;
            case Availability.CLUB_EXCLUSIVE:
                product.badgeText = terms.clubExclusive;
                product.badgeClass = "badge-info";
                break;
            default:
                product.badgeText = null;
                product.badgeClass = null;
        }

        product.footerAriaLabel = null;
        if (productAvailability === Availability.OUT_OF_STOCK ||
            productAvailability === Availability.SOLD_OUT ||
            productAvailability === Availability.PWS_BACKORDER_OVERRIDE ||
            productAvailability === Availability.BACKORDER_DO_NOT_ALLOW_ORDERING ||
            productAvailability === Availability.COMING_SOON ||
            productAvailability === Availability.CLUB_EXCLUSIVE_OUT_OF_STOCK ||
            productAvailability === Availability.CLUB_EXCLUSIVE_SOLD_OUT) {
            product.footerType = FooterType.LINK;
            product.footerText = forceSmall ? terms.viewDetailsMobileBtnText : viewDetailsButtonText;
        } else if (productAvailability === Availability.INACTIVE) {
            product.footerType = FooterType.REMOVE_FROM_LIST;
            product.footerText = terms.removeFromWishListBtnText;
        } else if (productDisplayType === DisplayType.BUNDLE) {
            product.footerType = FooterType.LINK;
            product.footerText = terms.buildBundle;
            product.footerAriaLabel = terms.buildBundlePrefix + " " + product.displayName + " " + terms.buildBundleSuffix;
        } else if (productDisplayType === DisplayType.BUDDY) {
            if (!shouldShowBagSidebar) {
                if (childProducts.length > 0) {
                    let childProductSkus = childProducts.map(cp => cp.sku);
                    product.groups = { [1]: childProductSkus };
                }
                product.footerType = FooterType.ADD_BUDDY_TO_CART;
                product.footerText = forceSmall ? "" : addToCartButtonText;
                product.footerAriaLabel = terms.ariaAddPrefix + " " + product.displayName + " " + terms.ariaAddSuffix;
            } else {
                product.footerType = FooterType.LINK;
                product.footerText = terms.buildBuddy;
            }
        } else if (productDisplayType === DisplayType.PRESELL) {
            product.footerType = FooterType.LINK;
            product.footerText = terms.presellBtnText;
        } else if (productDisplayType === DisplayType.SET) {
            product.footerType = FooterType.LINK;
            product.footerText = terms.buildSet;
            product.footerAriaLabel = terms.buildSetPrefix + " " + product.displayName + " " + terms.buildSetSuffix;
        } else {
            product.footerType = FooterType.ADD_TO_CART;
            product.footerText = forceSmall ? "" : addToCartButtonText;
            product.footerAriaLabel = terms.ariaAddPrefix + " " + product.displayName + " " + terms.ariaAddSuffix;
        }

        product.quantity = ko.observable(1);
        product.shouldShowBagSidebar = shouldShowBagSidebar;
        product.callbackFunction = callbackFunction;

        return product;
    };
}());
