import {
  ControlDefinition,
  serializeObject,
  ControlSerializationVisitor,
  isFunction
} from "@makeswift/controls";
import { RichTextV2Definition } from "../../rich-text-v2";
import { serializeFunction } from "./function-serialization";
class ClientMessagePortSerializationVisitor extends ControlSerializationVisitor {
  transferables = [];
  constructor() {
    const serializeFunctionPlugin = {
      match: isFunction,
      serialize: (val) => {
        const r = serializeFunction(val);
        this.transferables.push(r);
        return r;
      }
    };
    const serializeDefinitionPlugin = {
      match: (val) => val instanceof ControlDefinition,
      serialize: (val) => val.accept(this)
    };
    super([serializeFunctionPlugin, serializeDefinitionPlugin]);
  }
  getTransferables() {
    return [...this.transferables];
  }
  visitRichTextV2(def) {
    const { plugins, ...config } = def.config;
    const pluginDefs = plugins.map(
      ({ control }) => control ? {
        control: {
          definition: control.definition,
          // FIXME: remove getValue/onChange stubs once we released a version of the builder
          // built against the runtime where these can be optional
          getValue: () => void 0,
          onChange: () => {
          }
        }
      } : {}
    );
    const serialized = serializeObject(
      { config: { ...config, plugins: pluginDefs } },
      this.serializationPlugins
    );
    return { ...serialized, type: RichTextV2Definition.type };
  }
}
export {
  ClientMessagePortSerializationVisitor
};
//# sourceMappingURL=client-message-port-serialization-visitor.js.map