"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const commerce_1 = require("../../commerce");
(0, commerce_1.registerCommercePlugin)({
    name: 'Swell',
    id: '@builder.io/plugin-swell',
    settings: [
        {
            name: 'storeId',
            type: 'string',
            required: true,
            helperText: 'Get your Store ID from swell store settings https://swell.store/docs/api/?javascript#authentication',
        },
        {
            name: 'secretKey',
            type: 'string',
            required: true,
            helperText: 'Get your Secret key from swell store settings https://swell.store/docs/api/?javascript#authentication',
        },
    ],
    ctaText: `Connect your swell.is store`,
}, settings => {
    const basicCache = new Map();
    const baseUrl = (url) => 'https://builder.io/api/v1/proxy-api?url=' +
        encodeURIComponent('https://api.swell.store/' + url);
    const headers = {
        Authorization: `Basic ${btoa(`${settings.get('storeId')}:${settings.get('secretKey')}`)}`,
        Accept: 'application/json',
        'Content-Type': 'application/json',
    };
    const transformProduct = (product) => {
        var _a;
        return (Object.assign({ id: product.id, title: product.name, handle: product.slug }, (product.images && {
            image: {
                src: (_a = product.images[0]) === null || _a === void 0 ? void 0 : _a.file.url,
            },
        })));
    };
    return {
        product: {
            findById(id) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const key = `${id}productById`;
                    const product = basicCache.get(key) ||
                        (yield fetch(baseUrl(`products/${id}`), { headers })
                            .then(res => res.json())
                            .then(transformProduct));
                    basicCache.set(key, product);
                    return product;
                });
            },
            findByHandle(handle) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const key = `${handle}productByHandle`;
                    const response = basicCache.get(key) ||
                        (yield fetch(baseUrl(`products?where[active]=true&where[slug]=${handle}`), {
                            headers,
                        }).then(res => res.json()));
                    basicCache.set(key, response);
                    const product = response.results.map(transformProduct)[0];
                    return product;
                });
            },
            search(search) {
                return tslib_1.__awaiter(this, void 0, void 0, function* () {
                    const response = yield fetch(baseUrl(`products?where[active]=true&search=${search}`), {
                        headers,
                    }).then(res => res.json());
                    return response.results.map(transformProduct);
                });
            },
            getRequestObject(id) {
                return {
                    '@type': '@builder.io/core:Request',
                    request: {
                        url: baseUrl(`products/${id}`),
                    },
                    options: {
                        product: id,
                    },
                };
            },
        },
    };
});
//# sourceMappingURL=swell.js.map