"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractInterface = void 0;
const Semifunctional_1 = require("../../../Class/Semifunctional");
const weakMerge_1 = require("../../../weakMerge");
const wrap_1 = require("../../../wrap");
const Array_1 = require("../../../wrap/Array");
const Object_1 = require("../../../wrap/Object");
const SharedValue_1 = require("../../../wrap/SharedValue");
const _ids_1 = require("../../_ids");
function extractInterface(data) {
    if (typeof data === 'object') {
        if (data === null) {
            return null;
        }
        if (data instanceof Array) {
            return 'A';
        }
        else {
            return 'J';
        }
    }
    return null;
}
exports.extractInterface = extractInterface;
class State extends Semifunctional_1.Semifunctional {
    constructor(system) {
        super({
            fi: ['init'],
            fo: ['data'],
            i: ['done'],
            o: [],
        }, {
            output: {
                data: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_STATE);
    }
    f({ init }, done) {
        const sharedRef = { current: init };
        let api = (0, SharedValue_1.wrapSharedValue)(sharedRef, this.__system);
        const _ = extractInterface(init);
        if (_) {
            switch (_) {
                case 'A':
                    {
                        api = (0, weakMerge_1.weakMerge)(api, (0, Array_1.wrapSharedRefArrayInterface)(sharedRef, this.__system));
                    }
                    break;
                case 'J':
                    {
                        api = (0, weakMerge_1.weakMerge)(api, (0, Object_1.wrapSharedRef)(sharedRef, this.__system));
                    }
                    break;
            }
        }
        const data = (0, wrap_1.$wrap)(this.__system, api, _ ? ['V', _] : ['V']);
        done({
            data,
        });
    }
    onIterDataInputData(name, data) {
        // if (name === 'done') {
        this._backward('init');
        this._backward('done');
        // }
    }
}
exports.default = State;
//# sourceMappingURL=index.js.map