import { __read } from "tslib";
import * as React from "react";
import { valueOrDefault } from "@intelligentgraphics/ig.utilities";
import { useCatalogContext } from "./Context";
import { defaultFetchContent } from "./DefaultContentFetcher";
export var useCatalogView = function (props) {
    var catalogContext = useCatalogContext();
    var path = valueOrDefault(props.path, catalogContext.path);
    var addableTargetId = valueOrDefault(props.addableTargetId, catalogContext.addableTargetId);
    var _a = __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
            ? 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 = __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 = useCatalogView(props);
    return props.children(state);
};
CatalogView.displayName = "CatalogView";
export default CatalogView;
