"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReactifySearchLiquidFactory = void 0;
const react_1 = __importDefault(require("react"));
const ReactifySearchProvider_1 = require("../components/ReactifySearchProvider/ReactifySearchProvider");
class ReactifySearchLiquidFactory {
    constructor(providerProps) {
        this.providerProps = providerProps;
    }
    getProvider() {
        const providerProps = this.getProviderProps();
        const provider = (props) => (react_1.default.createElement(ReactifySearchProvider_1.ReactifySearchProvider, Object.assign({}, providerProps), props.children));
        return provider;
    }
    getMountNode() {
        const mountNode = document.getElementById(`reactify-mount-${this.providerProps.mode}`);
        if (!mountNode) {
            throw new Error(`Cannot find mount node with ID "reactify-mount-${this.providerProps.mode}"`);
        }
        return mountNode;
    }
    getData() {
        const dataNode = document.getElementById("reactify-data");
        if (!dataNode) {
            throw new Error(`Cannot find data node with ID "reactify-data"`);
        }
        try {
            const data = JSON.parse(dataNode.innerHTML);
            return data;
        }
        catch (_a) {
            throw new Error(`Cannot parse the data node with ID "reactify-data", check your liquid formatting`);
        }
    }
    getProviderProps() {
        var _a, _b, _c, _d;
        const shopifyPermanentDomain = (_a = this.providerProps.shopifyPermanentDomain) !== null && _a !== void 0 ? _a : (_b = window === null || window === void 0 ? void 0 : window.Shopify) === null || _b === void 0 ? void 0 : _b.shop;
        if (!shopifyPermanentDomain)
            throw new Error("Cannot determine shopifyPermanentDomain from window");
        const baseProps = Object.assign(Object.assign({}, this.providerProps), { shopifyPermanentDomain });
        if (this.providerProps.mode === "collection") {
            const collectionHandle = (_c = this.providerProps.collectionHandle) !== null && _c !== void 0 ? _c : (_d = window.location.pathname.split("/")) === null || _d === void 0 ? void 0 : _d[2];
            if (!collectionHandle)
                throw new Error("Cannot determine collectionHandle from URL");
            return Object.assign(Object.assign({}, baseProps), { mode: "collection", collectionHandle });
        }
        if (this.providerProps.mode === "curated-page") {
            const variation = this.providerProps.variation;
            if (variation === "collection") {
                const handle = this.providerProps.handle;
                if (!handle)
                    throw new Error("Handle is required for curated-page collection mode");
                return Object.assign(Object.assign({}, baseProps), { mode: "curated-page", variation: "collection", handle });
            }
            if (variation === "search") {
                const searchTerm = this.providerProps.searchTerm;
                if (!searchTerm)
                    throw new Error("SearchTerm is required for curated-page search mode");
                return Object.assign(Object.assign({}, baseProps), { mode: "curated-page", variation: "search", searchTerm });
            }
        }
        return baseProps;
    }
}
exports.ReactifySearchLiquidFactory = ReactifySearchLiquidFactory;
//# sourceMappingURL=liquid.js.map