import List from "../../../../../src/core/collection/list/List.js";
import { noop } from "../../../../../src/core/function/noop.js";
import { FieldDescriptor } from "../../FieldDescriptor.js";
import { FieldValueAdapter } from "../../FieldValueAdapter.js";
import { TypeEditor } from "../../TypeEditor.js";
import { ListEditor } from "../collection/ListEditor.js";

export class ArrayEditor extends TypeEditor {
    inline = false;

    __list_editor = new ListEditor();

    build(parent, field, registry) {

        /**
         * @type {Array}
         */
        const array = field.adapter.read(parent, field.name);


        const list = new List();
        list.data = array;
        list.length = array.length;

        // remap to list
        const surrogate_field = new FieldDescriptor();
        surrogate_field.type = List;

        if (field.schema) {

            surrogate_field.schema = Object.assign({}, field.schema, {
                type: List
            });
        }

        surrogate_field.name = field.name;
        surrogate_field.adapter = new FieldValueAdapter();
        surrogate_field.adapter.read = () => list;
        surrogate_field.adapter.write = noop;
        surrogate_field.adapter.writable = false;


        return this.__list_editor.build(parent, surrogate_field, registry);

    }
}
