(*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *)

(* AST handling and type setup for import/export *)

module Flow = Flow_js
open Reason
open Type

let mk_module_t cx reason =
  ModuleT
    ( reason,
      {
        exports_tmap = Context.make_export_map cx NameUtils.Map.empty;
        cjs_export = None;
        has_every_named_export = false;
      },
      Context.is_strict cx
    )

(**
 * When CommonJS modules set their export type, we do two things:
 *
 * (1) Set the type in the cjs_export slot of the ModuleT container
 *
 * (2) If the type is an object, mark it's properties as named exports, via
 *     CJSExtractNamedExportsT. (this is for convenience as part of our
 *     ES <-> CJS module interop semantics)
 *)
let mk_commonjs_module_t cx reason_exports_module reason export_t =
  let exporttypes =
    {
      exports_tmap = Context.make_export_map cx NameUtils.Map.empty;
      cjs_export = Some export_t;
      has_every_named_export = false;
    }
  in
  Tvar.mk_where cx reason (fun t ->
      Flow.flow
        cx
        ( export_t,
          CJSExtractNamedExportsT
            (reason, (reason_exports_module, exporttypes, Context.is_strict cx), t)
        )
  )

(* given a module name, return associated tvar in module map (failing if not
   found); e.g., used to find tvars associated with requires *after* all
   requires already have entries in the module map *)
let require_t_of_ref_unsafe cx ?(declare_module = false) (loc, mref) =
  if declare_module || Context.in_declare_module cx then
    Flow.get_builtin_module cx loc mref
  else
    Context.find_require cx loc

let get_module_t cx ?declare_module source =
  OpenT (require_t_of_ref_unsafe cx ?declare_module source)

let require cx ~legacy_interop require_loc module_ref module_t =
  let reason = mk_reason (RCommonJSExports module_ref) require_loc in
  let is_strict = Context.is_strict cx in
  Tvar.mk_where cx reason (fun t_out ->
      Flow.flow cx (module_t, CJSRequireT { reason; t_out; is_strict; legacy_interop })
  )

let import_ns cx reason module_t =
  let is_strict = Context.is_strict cx in
  Tvar.mk_where cx reason (fun t ->
      Flow.flow cx (module_t, ImportModuleNsT { reason; t; is_strict })
  )

(* Module exports are treated differently than `exports`. The latter is a
   variable that is implicitly set to the empty object at the top of a
   module. As such, properties can be added to it throughout the module,
   corresponding to the pattern exports.foo = ... for exporting foo. As it turns
   out, module.exports is the same object. A different pattern is to reset
   module.exports at the end of the module, like module.exports = { foo: ... }
   to export foo. This makes any properties added to the exports object
   redundant. Both these patterns are modeled by storing module.exports as an
   internal variable, initially set to the empty object, doing inference on a
   module, and then flowing module.exports to exports, so that whatever its
   final value is (initial object or otherwise) is checked against the type
   declared for exports or any other use of exports. *)

let cjs_clobber cx loc t =
  if Module_info.cjs_clobber (Context.module_info cx) loc then Type_env.set_module_exports cx t

let export cx = Module_info.export (Context.module_info cx)

let export_star cx = Module_info.export_star (Context.module_info cx)

let export_type cx = Module_info.export_type (Context.module_info cx)

let export_type_star cx = Module_info.export_type_star (Context.module_info cx)

let export_binding cx ?is_function name loc = function
  | Flow_ast.Statement.ExportValue ->
    let t =
      Type_env.get_var_declared_type
        ~lookup_mode:Type_env.LookupMode.ForValue
        ?is_declared_function:is_function
        cx
        name
        loc
    in
    export cx name loc t
  | Flow_ast.Statement.ExportType ->
    let t =
      Type_env.get_var_declared_type
        ~lookup_mode:Type_env.LookupMode.ForType
        ?is_declared_function:is_function
        cx
        name
        loc
    in
    export_type cx name (Some loc) t

(* After we have seen all the export statements in a module, this function will
 * calculate a ModuleT type (or a tvar that resolves to one) describing the
 * exports of a file.
 *
 * For CommonJS modules, this is fairly simple. We have the exported value
 * itself, plus any type exports. If the exported value is an object, we treat
 * the fields as named exports for ES module dependents.
 *
 * For ES modules, we have both named exports and "star" exports, which copy the
 * exports of one file into another. This can lead to conflits, which are
 * resolved carefully. Note that locally named exports always win, even if they
 * are followed by a star export that includes a conflicting name.
 *
 * Finally, both CJS and ES modules can export types, which also has a star
 * export variant. Conflicts are handled in the same way.
 *)
let mk_module_t =
  Module_info.(
    let copy_named_exports cx reason module_t (loc, from_ns) =
      let reason = repos_reason loc reason in
      Tvar.mk_where cx reason (fun tout ->
          Flow.flow cx (from_ns, CopyNamedExportsT (reason, module_t, tout))
      )
    in
    let copy_type_exports cx reason module_t (loc, from_ns) =
      let reason = repos_reason loc reason in
      Tvar.mk_where cx reason (fun tout ->
          Flow.flow cx (from_ns, CopyTypeExportsT (reason, module_t, tout))
      )
    in
    let copy_star_exports cx reason exports module_t =
      Module_info.fold_star2
        (copy_named_exports cx reason)
        (copy_type_exports cx reason)
        module_t
        exports
    in
    let export_named cx reason kind named module_t =
      Tvar.mk_where cx reason (fun tout ->
          Flow.flow cx (module_t, ExportNamedT (reason, named, kind, tout))
      )
    in
    fun cx reason loc ->
      let info = Context.module_info cx in
      match info.kind with
      | CJS _ ->
        Type_env.get_module_exports cx loc
        |> mk_commonjs_module_t cx reason reason
        |> export_named cx reason ExportType info.type_named
        |> copy_star_exports cx reason ([], info.type_star)
      | ES { named; star } ->
        mk_module_t cx reason
        |> export_named cx reason ExportValue named
        |> export_named cx reason ExportType info.type_named
        |> copy_star_exports cx reason (star, info.type_star)
  )
