module sdp.meta.metadoc;
template SiSUabstraction() {
  import
    std.getopt,
    std.file,
    std.path,
    std.process;
  import
    sdp.meta,
    sdp.meta.metadoc_summary,
    sdp.meta.metadoc_from_src,
    sdp.meta.conf_make_meta,
    // sdp.meta.conf_make_meta_native,
    sdp.meta.conf_make_meta_sdlang,
    sdp.meta.conf_make_meta_composite,
    sdp.meta.defaults,
    sdp.meta.doc_debugs,
    sdp.meta.read_config_files,
    sdp.meta.read_source_files,
    sdp.meta.rgx,
    sdp.output.hub,
    sdp.output.paths_source;
  mixin SiSUrgxInit;
  mixin SiSUregisters;
  mixin SiSUextractSDLang;
  mixin SiSUnode;
  mixin SiSUbiblio;
  mixin SiSUrgxInitFlags;
  mixin outputHub;
  enum headBody { header, body_content, insert_file_list, image_list }
  enum makeMeta { make, meta }
  enum docAbst  { doc_abstraction, section_keys, segnames, segnames_0_4, images }
  static auto rgx = Rgx();
  auto SiSUabstraction(E,O,M)(
    E _env,
    O _opt_action,
    M _manifest,
  ){
    auto sdl_root_config_document = configRead!()(_manifest, _env, "config_document"); // document config file
    auto sdl_root_config_local_site = configRead!()(_manifest, _env, "config_local_site"); // local site config
    auto conf_files_composite_make = confFilesSDLtoStruct!()(sdl_root_config_document, sdl_root_config_local_site);
    /+ ↓ read file (filename with path) +/
    /+ ↓ file tuple of header and content +/
    debug(steps) {
      writeln(__LINE__, ":", __FILE__,
        ": step1 commence → (get document header & body & insert file list & if needed image list)"
      );
    }
    auto _header_body_insertfilelist_imagelist =
      SiSUrawMarkupContent!()(_opt_action, _manifest.src_fn);
    static assert(!isTypeTuple!(_header_body_insertfilelist_imagelist));
    static assert(_header_body_insertfilelist_imagelist.length==4);
    debug(steps) {
      writeln(__LINE__, ":", __FILE__, ": step1 complete");
    }
    debug(header_and_body) {
      writeln(header);
      writeln(_header_body_insertfilelist_imagelist.length);
      writeln(_header_body_insertfilelist_imagelist.length[headBody.body_content][0]);
    }
    /+ ↓ split header into make and meta +/
    debug(steps) {
      writeln(__LINE__, ":", __FILE__,
        ": step2 commence → (doc header: make & meta as struct)"
       );
    }
    auto _make_and_meta_struct =
      docHeaderMakeAndMetaTupExtractAndConvertToStruct!()(conf_files_composite_make, _header_body_insertfilelist_imagelist[headBody.header]); // breakage ...
    debug(steps) {
      writeln(__LINE__, ":", __FILE__, ": step2 complete");
    }
    /+ ↓ document abstraction: process document, return abstraction as tuple +/
    debug(steps) {
      writeln(__LINE__, ":", __FILE__, ": step3 commence → (document abstraction (da); da keys; segnames; doc_matters)");
    }
    auto da = SiSUdocAbstraction!()(
      _header_body_insertfilelist_imagelist[headBody.body_content],
      _make_and_meta_struct,
      _opt_action,
    );
    static assert(!isTypeTuple!(da));
    static assert(da.length==5);
    auto doc_abstraction = da[docAbst.doc_abstraction]; /+ head ~ toc ~ body ~ endnotes_seg ~ glossary ~ bibliography ~ bookindex ~ blurb; +/
    auto _document_section_keys_sequenced = da[docAbst.section_keys];
    string[] _doc_html_segnames = da[docAbst.segnames];
    string[] _doc_epub_segnames_0_4 = da[docAbst.segnames_0_4];
    auto _images = da[docAbst.images];
    debug(steps) {
      writeln(__LINE__, ":", __FILE__, ": step3 complete");
    }
    debug(steps) {
      writeln(__LINE__, ":", __FILE__, ": step4 commence → (doc_matters)");
    }
    struct DocumentMatters {
      auto opt_action() {
        /+ getopt options, commandline instructions, raw
         - processing instructions --epub --html etc.
         - command line config instructions --output-path
        +/
        return _opt_action;
      }
      auto conf_make_meta() { // TODO meld with all make instructions
        auto _k = _make_and_meta_struct;
        return _k;
      }
      auto environment() {
        struct Env_ {
          auto pwd() {
            return _manifest.pwd;
          }
          auto home() {
            return _manifest.home;
          }
        }
        return Env_();
      }
      auto is_pod() {
        return _manifest.is_pod;
      }
      auto source_filename() {
        return _manifest.src_fn;
      }
      auto src_path_info() { // consider, reconsider?
        auto _k = SiSUpathsSRC!()(_manifest.pwd, _manifest.src_fn);
        return _k;
      }
      auto language() {
        return _manifest.src_lng;
      }
      auto output_path() {
        return _manifest.output_path;
      }
      auto pod_manifest_list_of_filenames() {
        return _manifest.pod_manifest_list_of_filenames;
      }
      auto pod_manifest_list_of_languages() {
        return _manifest.pod_manifest_list_of_languages;
      }
      auto pod_manifest_filename() {
        return _manifest.pod_manifest_filename;
      }
      auto pod_manifest_path() {
        return _manifest.pod_manifest_path;
      }
      auto pod_manifest_file_with_path() {
        return _manifest.pod_manifest_file_with_path;
      }
      auto pod_config_dirs() {
        return _manifest.pod_config_dirs;
      }
      auto pod_image_dirs() {
        return _manifest.pod_image_dirs;
      }
      auto file_insert_list() {
        string[] _k = _header_body_insertfilelist_imagelist[headBody.insert_file_list];
        return _k;
      }
      auto image_list() {
        return _images;
      }
      auto keys_seq() {
        /+ contains .seg & .scroll sequences +/
        auto _k = _document_section_keys_sequenced;
        return _k;
      }
      string[] segnames() {
        string[] _k = _doc_html_segnames;
        return _k;
      }
      string[] segnames_lv_0_to_4() {
        string[] _k = _doc_epub_segnames_0_4;
        return _k;
      }
    }
    auto doc_matters = DocumentMatters();
    debug(steps) {
      writeln(__LINE__, ":", __FILE__, ": step4 complete");
    }
    auto t = tuple(doc_abstraction, doc_matters);
    static assert(t.length==2);
    return t;
  }
}