"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = require("react");
const storage_libs_1 = require("./storage-libs");
const attach_storage_1 = require("../components/attach-storage");
const exenv_1 = __importDefault(require("exenv"));
;
const STATE = {
    //UNDEFINED: "UNDEFINED",
    LOADING: "LOADING",
    //SSR: "SSR",
    ERROR: "ERROR",
    RESPONSE: "RESPONSE"
};
exports.default = attach_storage_1.withStorageSsrRendering(function (_a) {
    var { renderSsr, config, isOffline, preloadedFiles, renderListResults } = _a, props = __rest(_a, ["renderSsr", "config", "isOffline", "preloadedFiles", "renderListResults"]);
    const hash = require('object-hash');
    const hashValue = hash({
        storageId: props.storageId,
        prefix: props.prefix ? props.prefix : "",
        mode: props.mode,
        data: props.data
    });
    //console.log("hashValue: ", hashValue);
    const [state, setState] = react_1.useState({ state: STATE.LOADING, data: undefined, setRefetch: true, ignorePreloaded: false });
    const refetch = () => {
        setState({ state: STATE.LOADING, data: undefined, setRefetch: true, ignorePreloaded: true });
    };
    const loadData = (onSuccess, onError) => {
        storage_libs_1.listFiles(props.storageId, props.prefix ? props.prefix : "", props.mode, props.data, onSuccess, onError, config, isOffline);
    };
    const getPreloaded = () => {
        if (state.ignorePreloaded) {
            return undefined;
        }
        if (!exenv_1.default.canUseDOM) {
            if (renderListResults) {
                const foundVal = renderListResults.find(el => el.hashValue == hashValue);
                if (foundVal) {
                    return foundVal;
                }
            }
            if (state.state === STATE.LOADING) {
                renderSsr(loadData, hashValue);
                return undefined;
            }
        }
        else {
            return preloadedFiles !== undefined ? preloadedFiles.find(el => el.hashValue == hashValue) : undefined;
        }
        return undefined;
    };
    const preloaded = getPreloaded();
    //console.log("preloaded: ", preloaded);
    if (preloaded && state.state === STATE.LOADING) {
        setState({
            state: STATE.RESPONSE,
            data: {
                data: preloaded.data,
                files: preloaded.files,
            },
            setRefetch: state.setRefetch,
            ignorePreloaded: state.ignorePreloaded
        });
    }
    ;
    //console.log("state: ", state.data);
    react_1.useEffect(() => {
        if (props.onSetRefetch && state.setRefetch /*state.state === STATE.LOADING /*!isRefetchSet*/) {
            props.onSetRefetch(() => refetch);
        }
        if (state.state === STATE.LOADING) {
            loadData(({ data, files, folders }) => {
                //console.log(data, files, folders)
                setState({
                    state: STATE.RESPONSE,
                    data: {
                        data: data,
                        files: files,
                    },
                    setRefetch: false,
                    ignorePreloaded: state.ignorePreloaded
                });
            }, (err) => {
                setState({
                    state: STATE.RESPONSE,
                    data: err,
                    setRefetch: false,
                    ignorePreloaded: state.ignorePreloaded
                });
            });
        }
        else if (state.setRefetch) {
            setState({
                state: state.state,
                data: state.data,
                setRefetch: false,
                ignorePreloaded: state.ignorePreloaded
            });
        }
    }, [state]);
    return props.children({
        loading: state.state === STATE.LOADING /*|| state.state === STATE.SSR*/,
        data: state.state === STATE.RESPONSE ? state.data.data : undefined,
        files: state.state === STATE.RESPONSE ? state.data.files : undefined,
        error: state.state === STATE.ERROR ? state.data : undefined,
    });
});
//# sourceMappingURL=files-list.js.map