"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var control_serialization_exports = {};
__export(control_serialization_exports, {
  deserializeControl: () => deserializeControl,
  deserializeControls: () => deserializeControls,
  deserializeLegacyControl: () => deserializeLegacyControl,
  deserializeUnifiedControlDef: () => deserializeUnifiedControlDef,
  isSerializedControl: () => isSerializedControl,
  serializeControl: () => serializeControl,
  serializeControls: () => serializeControls
});
module.exports = __toCommonJS(control_serialization_exports);
var import_controls = require("@makeswift/controls");
var import_controls2 = require("../../controls");
var import_deleted = require("../../prop-controllers/deleted");
var import_function_serialization = require("./function-serialization");
var import_prop_controllers2 = require("@makeswift/prop-controllers");
var import_descriptors2 = require("../../prop-controllers/descriptors");
var import_controls3 = require("@makeswift/controls");
var import_message_port_serializer = require("../../controls/visitors/message-port-serializer");
function serializeShapeControl(control) {
  const { type } = control.options;
  const transferables = [];
  const serializedType = {};
  Object.entries(type).forEach(([key, control2]) => {
    const [serializedControl, serializedControlTransferables] = serializeControl(control2);
    serializedType[key] = serializedControl;
    transferables.push(...serializedControlTransferables);
  });
  return [{ ...control, options: { ...control.options, type: serializedType } }, transferables];
}
function deserializeShapeControl(control) {
  const { type } = control.options;
  const deserializedType = {};
  Object.entries(type).forEach(([key, control2]) => {
    deserializedType[key] = deserializeControl(
      control2
    );
  });
  return { ...control, options: { ...control.options, type: deserializedType } };
}
function serializeListControl(control) {
  const { type, getItemLabel } = control.options;
  const transferables = [];
  const [serializedType, serializedTypeTransferables] = serializeControl(type);
  const serializedGetItemLabel = getItemLabel && (0, import_function_serialization.serializeFunction)(getItemLabel);
  transferables.push(...serializedTypeTransferables);
  if (serializedGetItemLabel != null)
    transferables.push(serializedGetItemLabel);
  return [
    {
      ...control,
      options: {
        ...control.options,
        type: serializedType,
        getItemLabel: serializedGetItemLabel
      }
    },
    transferables
  ];
}
function deserializeListControl(serializedControl) {
  const { type, getItemLabel } = serializedControl.options;
  const deserializedType = deserializeControl(type);
  const deserializedGetItemLabel = getItemLabel && (0, import_function_serialization.deserializeFunction)(getItemLabel);
  return {
    ...serializedControl,
    options: {
      ...serializedControl.options,
      type: deserializedType,
      getItemLabel: deserializedGetItemLabel
    }
  };
}
function serializeTypeaheadControl(control) {
  const { getItems } = control.options;
  const serializedGetItems = getItems && (0, import_function_serialization.serializeFunction)(getItems);
  return [
    { ...control, options: { ...control.options, getItems: serializedGetItems } },
    serializedGetItems == null ? [] : [serializedGetItems]
  ];
}
function deserializeTypeaheadControl(serializedControl) {
  const { getItems } = serializedControl.options;
  const deserializedGetItems = getItems && (0, import_function_serialization.deserializeFunction)(getItems);
  return {
    ...serializedControl,
    options: { ...serializedControl.options, getItems: deserializedGetItems }
  };
}
function serializeControlDef(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeControlDef(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeGapYControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeGapYControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeCheckboxControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeCheckboxControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeResponsiveColorControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeResponsiveColorControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeNumberControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeNumberControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeDateControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeDateControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeLinkControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeLinkControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeTextInputControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeTextInputControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeResponsiveLengthControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeResponsiveLengthControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeTextStyleControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeTextStyleControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeImageControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeImageControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeRichTextControl(control) {
  const { options } = control;
  if (typeof options !== "function")
    return [{ ...control, options }, []];
  const serializedOptions = (0, import_function_serialization.serializeFunction)(options);
  return [{ ...control, options: serializedOptions }, [serializedOptions]];
}
function deserializeRichTextControl(serializedControl) {
  const { options } = serializedControl;
  if (!(0, import_function_serialization.isSerializedFunction)(options))
    return { ...serializedControl, options };
  const deserializedOptions = (0, import_function_serialization.deserializeFunction)(options);
  return { ...serializedControl, options: deserializedOptions };
}
function serializeControl(control) {
  if ((0, import_descriptors2.isLegacyDescriptor)(control)) {
    return serializeLegacyControl(control);
  }
  const messagePortVisitor = new import_message_port_serializer.ClientMessagePortSerializationVisitor();
  const serializedControl = control.accept(messagePortVisitor);
  return [serializedControl, messagePortVisitor.getTransferables()];
}
function isSerializedControl(control) {
  return control != null && typeof control === "object" && "type" in control && typeof control.type === "string";
}
function serializeLegacyControl(control) {
  switch (control.type) {
    case import_prop_controllers2.Types.Checkbox:
      return serializeCheckboxControl(control);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.List:
      return serializeListControl(control);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.Shape:
      return serializeShapeControl(control);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.Typeahead:
      return serializeTypeaheadControl(control);
    case import_prop_controllers2.Types.GapX:
      return serializeControlDef(control);
    case import_prop_controllers2.Types.GapY:
      return serializeGapYControl(control);
    case import_prop_controllers2.Types.ResponsiveColor:
      return serializeResponsiveColorControl(control);
    case import_prop_controllers2.Types.ResponsiveNumber:
      return serializeControlDef(control);
    case import_prop_controllers2.Types.Number:
      return serializeNumberControl(control);
    case import_prop_controllers2.Types.ResponsiveIconRadioGroup:
      return serializeControlDef(control);
    case import_prop_controllers2.Types.ResponsiveSelect:
      return serializeControlDef(control);
    case import_prop_controllers2.Types.ResponsiveLength:
      return serializeResponsiveLengthControl(control);
    case import_prop_controllers2.Types.Date:
      return serializeDateControl(control);
    case import_prop_controllers2.Types.Link:
      return serializeLinkControl(control);
    case import_prop_controllers2.Types.TextInput:
      return serializeTextInputControl(control);
    case import_prop_controllers2.Types.TextStyle:
      return serializeTextStyleControl(control);
    case import_prop_controllers2.Types.Image:
      return serializeImageControl(control);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.RichText:
      return serializeRichTextControl(control);
    default:
      return [control, []];
  }
}
function isSerializedLegacyControl(control) {
  return "options" in control;
}
function deserializeLegacyControl(serializedControl) {
  switch (serializedControl.type) {
    case import_prop_controllers2.Types.Checkbox:
      return deserializeCheckboxControl(serializedControl);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.List:
      return deserializeListControl(serializedControl);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.Shape:
      return deserializeShapeControl(serializedControl);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.Typeahead:
      return deserializeTypeaheadControl(serializedControl);
    case import_prop_controllers2.Types.GapX:
      return deserializeControlDef(serializedControl);
    case import_prop_controllers2.Types.GapY:
      return deserializeGapYControl(serializedControl);
    case import_prop_controllers2.Types.ResponsiveColor:
      return deserializeResponsiveColorControl(serializedControl);
    case import_prop_controllers2.Types.ResponsiveNumber:
      return deserializeControlDef(serializedControl);
    case import_prop_controllers2.Types.Number:
      return deserializeNumberControl(serializedControl);
    case import_prop_controllers2.Types.ResponsiveIconRadioGroup:
      return deserializeControlDef(serializedControl);
    case import_prop_controllers2.Types.ResponsiveSelect:
      return deserializeControlDef(serializedControl);
    case import_prop_controllers2.Types.ResponsiveLength:
      return deserializeResponsiveLengthControl(serializedControl);
    case import_prop_controllers2.Types.Date:
      return deserializeDateControl(serializedControl);
    case import_prop_controllers2.Types.Link:
      return deserializeLinkControl(serializedControl);
    case import_prop_controllers2.Types.TextInput:
      return deserializeTextInputControl(serializedControl);
    case import_prop_controllers2.Types.TextStyle:
      return deserializeTextStyleControl(serializedControl);
    case import_prop_controllers2.Types.Image:
      return deserializeImageControl(serializedControl);
    case import_deleted.DELETED_PROP_CONTROLLER_TYPES.RichText:
      return deserializeRichTextControl(serializedControl);
    default:
      return serializedControl;
  }
}
function deserializeControl(serializedControl) {
  if (isSerializedLegacyControl(serializedControl)) {
    return deserializeLegacyControl(serializedControl);
  }
  return deserializeUnifiedControlDef(
    (0, import_controls3.deserializeRecord)(serializedControl, [import_message_port_serializer.functionDeserializationPlugin])
  );
}
function deserializeUnifiedControlDef(record) {
  const deserializeMethod = {
    [import_controls2.CheckboxDefinition.type]: import_controls2.CheckboxDefinition.deserialize,
    [import_controls2.ColorDefinition.type]: import_controls2.ColorDefinition.deserialize,
    [import_controls2.ComboboxDefinition.type]: import_controls2.ComboboxDefinition.deserialize,
    [import_controls2.FontDefinition.type]: import_controls2.FontDefinition.deserialize,
    [import_controls2.GroupDefinition.type]: (record2) => import_controls2.GroupDefinition.deserialize(record2, deserializeUnifiedControlDef),
    [import_controls2.IconRadioGroupDefinition.type]: import_controls2.IconRadioGroupDefinition.deserialize,
    [import_controls2.ImageDefinition.type]: import_controls2.ImageDefinition.deserialize,
    [import_controls2.LinkDefinition.type]: import_controls2.LinkDefinition.deserialize,
    [import_controls2.ListDefinition.type]: (record2) => import_controls2.ListDefinition.deserialize(record2, deserializeUnifiedControlDef),
    [import_controls2.NumberDefinition.type]: import_controls2.NumberDefinition.deserialize,
    [import_controls2.RichTextV1Definition.type]: import_controls2.RichTextV1Definition.deserialize,
    [import_controls2.RichTextV2Definition.type]: (record2) => import_controls2.RichTextV2Definition.deserialize(record2, deserializeUnifiedControlDef),
    [import_controls2.SelectDefinition.type]: import_controls2.SelectDefinition.deserialize,
    [import_controls2.ShapeDefinition.type]: (record2) => import_controls2.ShapeDefinition.deserialize(record2, deserializeUnifiedControlDef),
    [import_controls.ShapeV2Definition.type]: (record2) => import_controls.ShapeV2Definition.deserialize(record2, deserializeUnifiedControlDef),
    [import_controls2.SlotDefinition.type]: import_controls2.SlotDefinition.deserialize,
    [import_controls2.StyleDefinition.type]: import_controls2.StyleDefinition.deserialize,
    [import_controls2.StyleV2Definition.type]: (record2) => import_controls2.StyleV2Definition.deserialize(record2, deserializeUnifiedControlDef),
    [import_controls2.TextAreaDefinition.type]: import_controls2.TextAreaDefinition.deserialize,
    [import_controls2.TextInputDefinition.type]: import_controls2.TextInputDefinition.deserialize,
    [import_controls2.unstable_TypographyDefinition.type]: import_controls2.unstable_TypographyDefinition.deserialize
  };
  const deserialize = deserializeMethod[record.type] ?? null;
  if (deserialize == null) {
    throw new Error(`Unknown control type: ${record.type}`);
  }
  return deserialize(record);
}
function serializeControls(controls) {
  return Object.entries(controls).reduce(
    ([accControls, accTransferables], [key, control]) => {
      const [serializedControl, transferables] = serializeControl(control);
      return [{ ...accControls, [key]: serializedControl }, [...accTransferables, ...transferables]];
    },
    [{}, []]
  );
}
function deserializeControls(serializedControls, {
  onError
} = {}) {
  return Object.entries(serializedControls).reduce(
    (deserializedControls, [key, serializedControl]) => {
      try {
        if (!isSerializedControl(serializedControl)) {
          throw new Error(
            `Expected serialized control data, got ${JSON.stringify(serializedControl)}`
          );
        }
        const deserializedControl = deserializeControl(serializedControl);
        return { ...deserializedControls, [key]: deserializedControl };
      } catch (err) {
        const error = err instanceof Error ? new Error(`Could not deserialize control for "${key}": ${err.message}`, {
          cause: err
        }) : new Error(`Could not deserialize control for "${key}", unknown error: ${err}`);
        onError?.(error, { key, serializedControl });
        return deserializedControls;
      }
    },
    {}
  );
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  deserializeControl,
  deserializeControls,
  deserializeLegacyControl,
  deserializeUnifiedControlDef,
  isSerializedControl,
  serializeControl,
  serializeControls
});
//# sourceMappingURL=control-serialization.js.map