{"version":3,"file":"swiftPackageModule.js","names":[],"sources":["esm/vite/plugins/swiftPackageModule.js"],"sourcesContent":["import { existsSync, readFileSync } from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport { dirname, join } from \"node:path\";\nimport { swiftPackageDirOf, swiftPackageId } from \"vxrn/swift-package-id\";\nimport { methodSubscribed, methodWired, renderMethodCall, renderViewWires, toNativeSourceStructs } from \"./nativeSourceWires\";\nfunction renderSwiftPackageModule(id, platform, root) {\n  if (platform !== \"ios\") {\n    throw new Error(\n      `[one] ${id} is Swift source and cannot be imported into an Android build; use an .android.ts entry`\n    );\n  }\n  const appManifest = join(root, \"package.json\");\n  const app = existsSync(appManifest) ? JSON.parse(readFileSync(appManifest, \"utf8\")) : {};\n  const packageDir = swiftPackageDirOf(id);\n  if (!packageDir) {\n    throw new Error(\n      `[one] ${id} is not inside a swift package (no Package.swift above it)`\n    );\n  }\n  const packageName = swiftPackageId(packageDir);\n  if (Object.hasOwn(app.dependencies ?? {}, \"expo\") || Object.hasOwn(app.devDependencies ?? {}, \"expo\")) {\n    const podspec = join(root, \"ios\", \"OneSwiftPackages\", packageName, `${packageName}.podspec`);\n    if (!existsSync(podspec)) {\n      throw new Error(\n        `[one] ${id} needs one prebuild, or Expo prebuild with nativeSource enabled in the vxrn config plugin and ios generated`\n      );\n    }\n  }\n  const requireFromFile = createRequire(id);\n  const { writeNativeSourceDeclaration, writeSwiftPackageArtifacts } = requireFromFile(\n    \"vxrn/native-source-contract\"\n  );\n  const contract = writeNativeSourceDeclaration(id);\n  const artifacts = writeSwiftPackageArtifacts(packageDir);\n  const hash = artifacts.hash;\n  const watchFiles = Object.keys(artifacts.files).map((file) => join(packageDir, file));\n  const fill = /@main\\s+struct\\s+\\w+\\s*:\\s*(SwiftUI\\.)?App\\b/.test(\n    readFileSync(id, \"utf8\")\n  );\n  const oneRoot = dirname(requireFromFile.resolve(\"one/package.json\"));\n  const swiftHost = join(oneRoot, \"dist/esm/platform/swift/index.native.js\");\n  const nativeSource = join(oneRoot, \"dist/esm/platform/native-source/index.native.js\");\n  const wireModule = join(oneRoot, \"dist/esm/platform/native-source/wire.native.js\");\n  const eventsModule = join(oneRoot, \"dist/esm/platform/native-source/events.native.js\");\n  const needsWire = contract.modules.some((module) => module.methods.some(methodWired));\n  const needsEvents = contract.modules.some((module) => module.methods.some(methodSubscribed));\n  const structs = needsWire || needsEvents ? `\nconst nativeSourceStructs = ${JSON.stringify(toNativeSourceStructs(contract.structs))}\n` : \"\";\n  const exports = contract.modules.map(\n    (module) => `export const ${module.name} = Object.freeze({${module.methods.map((method) => renderMethodCall(`${packageName}.${module.name}`, method, hash)).join(\",\")}})`\n  ).join(\"\\n\");\n  const views = contract.views.map(\n    (view) => `export function ${view.name}(props) { return createElement(SwiftPackageView, { packageName: ${JSON.stringify(packageName)}, view: ${JSON.stringify(view.name)}, contractHash: ${JSON.stringify(hash)}, props, fill: false${renderViewWires(view, contract.structs)} }) }`\n  ).join(\"\\n\");\n  const code = `${contract.modules.length > 0 ? `import { callNativeSource } from ${JSON.stringify(nativeSource)}\n` : \"\"}${needsWire ? `import { encodeNativeSourceArgs, decodeNativeSourceResult } from ${JSON.stringify(wireModule)}\n` : \"\"}${needsEvents ? `import { subscribeNativeSourceEvent } from ${JSON.stringify(eventsModule)}\n` : \"\"}${contract.defaultView || views ? `import { createElement } from 'react'\nimport { SwiftPackageView } from ${JSON.stringify(swiftHost)}\n` : \"\"}${structs}${contract.defaultView ? `export default function SwiftPackage(props) {\n  return createElement(SwiftPackageView, { packageName: ${JSON.stringify(packageName)}, view: '', contractHash: '', props, fill: ${fill} })\n}\n` : \"\"}${exports}\n${views}\n`;\n  return { code, watchFiles };\n}\nexport {\n  renderSwiftPackageModule\n};\n//# sourceMappingURL=swiftPackageModule.js.map\n"],"mappings":";;;;;;;AAKA,SAAS,yBAAyB,IAAI,UAAU,MAAM;CACpD,IAAI,aAAa,OAAO;EACtB,MAAM,IAAI,MACR,SAAS,GAAG,wFACd;CACF;CACA,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