"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var React = require("react");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var Context_1 = require("./Context");
var DefaultContentFetcher_1 = require("./DefaultContentFetcher");
exports.useCatalogView = function (props) {
    var catalogContext = Context_1.useCatalogContext();
    var path = ig_utilities_1.valueOrDefault(props.path, catalogContext.path);
    var addableTargetId = ig_utilities_1.valueOrDefault(props.addableTargetId, catalogContext.addableTargetId);
    var _a = tslib_1.__read(React.useState({
        loading: true,
        data: undefined,
        error: undefined,
        path: path,
        addableTargetId: addableTargetId,
    }), 2), state = _a[0], setState = _a[1];
    var fetchParams = React.useMemo(function () { return ({
        path: path,
        addableTargetId: addableTargetId,
        flat: props.flat,
        filterContent: props.filterContent,
        data: props.data,
    }); }, [
        catalogContext,
        addableTargetId,
        props.fetchContent,
        props.filterContent,
        props.flat,
        path,
        props.data,
    ]);
    React.useEffect(function () {
        var fetchContent = props.fetchContent === undefined
            ? DefaultContentFetcher_1.defaultFetchContent
            : props.fetchContent;
        var canceled = false;
        try {
            var fetchResult = fetchContent(fetchParams, catalogContext.catalogFetcher);
            if (typeof fetchResult === "object" && "then" in fetchResult) {
                setState({
                    loading: true,
                    error: undefined,
                    data: state.data,
                    path: state.path,
                    addableTargetId: state.addableTargetId,
                });
                var fetchResultPromise = fetchResult;
                fetchResultPromise
                    .then(function (data) { return [undefined, data]; })
                    .catch(function (err) { return [err, undefined]; })
                    .then(function (_a) {
                    var _b = tslib_1.__read(_a, 2), err = _b[0], data = _b[1];
                    if (canceled) {
                        return;
                    }
                    if (data !== undefined &&
                        props.onPathContentFetched !== undefined) {
                        props.onPathContentFetched(data, fetchParams.data);
                    }
                    setState({
                        error: err,
                        data: data,
                        loading: false,
                        path: path,
                        addableTargetId: addableTargetId,
                    });
                });
            }
            else {
                if (props.onPathContentFetched !== undefined) {
                    props.onPathContentFetched(fetchResult, fetchParams.data);
                }
                setState({
                    data: fetchResult,
                    loading: false,
                    error: undefined,
                    path: path,
                    addableTargetId: addableTargetId,
                });
            }
        }
        catch (err) {
            setState({
                data: undefined,
                loading: false,
                error: err,
                path: path,
                addableTargetId: addableTargetId,
            });
        }
        return function () {
            canceled = true;
        };
    }, [fetchParams]);
    return state;
};
var CatalogView = function (props) {
    var state = exports.useCatalogView(props);
    return props.children(state);
};
CatalogView.displayName = "CatalogView";
exports.default = CatalogView;
