"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var integration_body_exports = {};
__export(integration_body_exports, {
  prepareCreateIntegrationBody: () => prepareCreateIntegrationBody,
  prepareUpdateIntegrationBody: () => prepareUpdateIntegrationBody
});
module.exports = __toCommonJS(integration_body_exports);
var utils = __toESM(require("../utils"));
const prepareCreateIntegrationBody = async (integration) => ({
  name: integration.name,
  version: integration.version,
  title: "title" in integration ? integration.title : void 0,
  description: "description" in integration ? integration.description : void 0,
  user: integration.user,
  events: integration.events ? await utils.records.mapValuesAsync(integration.events, async (event) => ({
    ...event,
    schema: await utils.schema.mapZodToJsonSchema(event, {
      useLegacyZuiTransformer: integration.__advanced?.useLegacyZuiTransformer
    })
  })) : void 0,
  actions: integration.actions ? await utils.records.mapValuesAsync(integration.actions, async (action) => ({
    ...action,
    input: {
      ...action.input,
      schema: await utils.schema.mapZodToJsonSchema(action.input, {
        useLegacyZuiTransformer: integration.__advanced?.useLegacyZuiTransformer
      })
    },
    output: {
      ...action.output,
      schema: await utils.schema.mapZodToJsonSchema(action.output, {
        useLegacyZuiTransformer: integration.__advanced?.useLegacyZuiTransformer
      })
    }
  })) : void 0,
  channels: integration.channels ? await utils.records.mapValuesAsync(integration.channels, async (channel) => ({
    ...channel,
    messages: await utils.records.mapValuesAsync(channel.messages, async (message) => ({
      ...message,
      schema: await utils.schema.mapZodToJsonSchema(message, {
        useLegacyZuiTransformer: integration.__advanced?.useLegacyZuiTransformer
      })
    }))
  })) : void 0,
  states: integration.states ? await utils.records.mapValuesAsync(integration.states, async (state) => ({
    ...state,
    schema: await utils.schema.mapZodToJsonSchema(state, {
      useLegacyZuiTransformer: integration.__advanced?.useLegacyZuiTransformer
    })
  })) : void 0,
  entities: integration.entities ? await utils.records.mapValuesAsync(integration.entities, async (entity) => ({
    ...entity,
    schema: await utils.schema.mapZodToJsonSchema(entity, {
      useLegacyZuiTransformer: integration.__advanced?.useLegacyZuiTransformer
    })
  })) : void 0,
  attributes: integration.attributes,
  extraOperations: "__advanced" in integration ? integration.__advanced?.extraOperations : void 0
});
const prepareUpdateIntegrationBody = (localIntegration, remoteIntegration) => {
  const actions = utils.attributes.prepareAttributeUpdateBody({
    localItems: utils.records.setNullOnMissingValues(localIntegration.actions, remoteIntegration.actions),
    remoteItems: remoteIntegration.actions
  });
  const events = utils.attributes.prepareAttributeUpdateBody({
    localItems: utils.records.setNullOnMissingValues(localIntegration.events, remoteIntegration.events),
    remoteItems: remoteIntegration.events
  });
  const states = utils.records.setNullOnMissingValues(localIntegration.states, remoteIntegration.states);
  const entities = utils.records.setNullOnMissingValues(localIntegration.entities, remoteIntegration.entities);
  const user = {
    ...localIntegration.user,
    tags: utils.records.setNullOnMissingValues(localIntegration.user?.tags, remoteIntegration.user?.tags)
  };
  const channels = _prepareUpdateIntegrationChannelsBody(localIntegration.channels ?? {}, remoteIntegration.channels);
  const interfaces = utils.records.setNullOnMissingValues(localIntegration.interfaces, remoteIntegration.interfaces);
  const configurations = utils.records.setNullOnMissingValues(
    localIntegration.configurations,
    remoteIntegration.configurations
  );
  const readme = localIntegration.readme;
  const icon = localIntegration.icon;
  const attributes = utils.records.setNullOnMissingValues(localIntegration.attributes, remoteIntegration.attributes);
  const extraOperations = localIntegration.extraOperations;
  return {
    ..._maybeRemoveVrlScripts(localIntegration, remoteIntegration),
    actions,
    events,
    states,
    entities,
    user,
    channels,
    interfaces,
    configurations,
    readme,
    icon,
    attributes,
    extraOperations
  };
};
const _maybeRemoveVrlScripts = (localIntegration, remoteIntegration) => {
  const newIntegration = structuredClone(localIntegration);
  if (remoteIntegration.configuration?.identifier?.linkTemplateScript && !localIntegration.configuration?.identifier?.linkTemplateScript) {
    newIntegration.configuration ??= remoteIntegration.configuration;
    newIntegration.configuration.identifier ??= remoteIntegration.configuration.identifier;
    newIntegration.configuration.identifier.linkTemplateScript = null;
    newIntegration.configuration.identifier.required = false;
  }
  if (remoteIntegration.identifier.extractScript && !localIntegration.identifier?.extractScript) {
    newIntegration.identifier ??= remoteIntegration.identifier;
    newIntegration.identifier.extractScript = null;
  }
  if (remoteIntegration.identifier.fallbackHandlerScript && !localIntegration.identifier?.fallbackHandlerScript) {
    newIntegration.identifier ??= remoteIntegration.identifier;
    newIntegration.identifier.fallbackHandlerScript = null;
  }
  for (const configName of Object.keys(localIntegration.configurations ?? {})) {
    if (remoteIntegration.configurations[configName]?.identifier.linkTemplateScript && !localIntegration.configurations?.[configName]?.identifier?.linkTemplateScript) {
      newIntegration.configurations ??= remoteIntegration.configurations;
      newIntegration.configurations[configName] ??= remoteIntegration.configurations[configName];
      newIntegration.configurations[configName].identifier ??= remoteIntegration.configurations[configName].identifier;
      newIntegration.configurations[configName].identifier.linkTemplateScript = null;
      newIntegration.configurations[configName].identifier.required = false;
    }
  }
  return newIntegration;
};
const _prepareUpdateIntegrationChannelsBody = (localChannels, remoteChannels) => {
  const channelBody = {};
  const zipped = utils.records.zipObjects(localChannels, remoteChannels);
  for (const [channelName, [localChannel, remoteChannel]] of Object.entries(zipped)) {
    if (localChannel && remoteChannel) {
      channelBody[channelName] = _prepareUpdateIntegrationChannelBody(localChannel, remoteChannel);
    } else if (localChannel) {
      channelBody[channelName] = localChannel;
      continue;
    } else if (remoteChannel) {
      channelBody[channelName] = null;
      continue;
    }
  }
  return channelBody;
};
const _prepareUpdateIntegrationChannelBody = (localChannel, remoteChannel) => ({
  ...localChannel,
  messages: utils.records.setNullOnMissingValues(localChannel?.messages, remoteChannel.messages),
  message: {
    ...localChannel?.message,
    tags: utils.records.setNullOnMissingValues(localChannel?.message?.tags, remoteChannel.message.tags)
  },
  conversation: {
    ...localChannel?.conversation,
    tags: utils.records.setNullOnMissingValues(localChannel?.conversation?.tags, remoteChannel.conversation.tags)
  }
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  prepareCreateIntegrationBody,
  prepareUpdateIntegrationBody
});
//# sourceMappingURL=integration-body.js.map
