"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 components_1 = require("../components");
class ReactifySearchLiquidFactory {
    constructor(providerProps) {
        this.providerProps = providerProps;
    }
    getProvider() {
        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 collectionHandle = (_c = this.providerProps.collectionHandle) !== null && _c !== void 0 ? _c : (_d = window.location.pathname.split("/")) === null || _d === void 0 ? void 0 : _d[2];
        if (this.providerProps.mode === "collection" && !collectionHandle) {
            throw new Error("Cannot determine collectionHandle from URL");
        }
        const provider = (props) => (react_1.default.createElement(components_1.ReactifySearchProvider, Object.assign({}, this.providerProps, { shopifyPermanentDomain: shopifyPermanentDomain, collectionHandle: collectionHandle }), 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`);
        }
    }
}
exports.ReactifySearchLiquidFactory = ReactifySearchLiquidFactory;
//# sourceMappingURL=liquid.js.map